Advertisement
Guest User

KillNotResponding

a guest
Mar 12th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace KillNotResponding
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.  
  11.             //Variables for settings
  12.             bool write_log = false, show_ui = false;
  13.  
  14.             //Get the commandline arguments
  15.             foreach (string arg in args)
  16.             {
  17.  
  18.                 //Trim the arguments of trailing slashes
  19.                 string trimmed_arg = arg.Replace("/", "");
  20.  
  21.                 //Assign the correct settings as determined by the arguments
  22.                 if (trimmed_arg == "UI")
  23.                 {
  24.                     show_ui = true;
  25.                 }
  26.                 else if (trimmed_arg == "LOG")
  27.                 {
  28.                     write_log = true;
  29.                 }
  30.             }
  31.  
  32.             //Enumerate the running processes
  33.             System.Diagnostics.Process[] pList = System.Diagnostics.Process.GetProcesses();
  34.             bool program_was_ended = false;
  35.  
  36.             //Iterate through list of processes
  37.             foreach (System.Diagnostics.Process proc in pList)
  38.             {
  39.  
  40.                 try
  41.                 {
  42.                     //Check if process is responding
  43.                     if (proc.Responding == false)
  44.                     {
  45.  
  46.                         //Kill the selected process
  47.                         proc.Kill();
  48.                         Console.WriteLine("Ended "  + " " + proc.ProcessName + ".exe (" + proc.MainWindowTitle + ") with ID:" + proc.Id + " at " + DateTime.Now.TimeOfDay);
  49.                         program_was_ended = true;
  50.  
  51.  
  52.                         //Log the kill if requested
  53.                         if (write_log == true)
  54.                         {
  55.                             StreamWriter SW = new StreamWriter(Environment.CurrentDirectory + "\\kill-log.txt");
  56.                             SW.WriteLine("Ended " + " " + proc.ProcessName + ".exe (" + proc.MainWindowTitle + ") with ID:" + proc.Id + " at " + DateTime.Now.TimeOfDay);
  57.                             SW.Close();
  58.                         }
  59.  
  60.                     }
  61.  
  62.                     //Catch any exceptions
  63.                 }
  64.                 catch (Exception ex)
  65.                 {
  66.                     Console.WriteLine(ex.Message);
  67.                 }
  68.             }
  69.  
  70.             //Keep the console window active
  71.             if (show_ui == true)
  72.             {
  73.                 if (program_was_ended == false)
  74.                 {
  75.                     Console.WriteLine("No non-responsive programs were ended.");
  76.                 }
  77.                 Console.ReadKey();
  78.             }
  79.  
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement