Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. foreach (Process p in Process.GetProcessesByName("explorer"))
  2. {
  3. p.Kill();
  4. }
  5.  
  6. var processName = "notepad.exe";
  7.  
  8. var connectoptions = new ConnectionOptions();
  9. connectoptions.Username = @"YourDomainNameUserName";
  10. connectoptions.Password = "User Password";
  11.  
  12. string ipAddress = "192.168.0.100";
  13. ManagementScope scope = new ManagementScope(@"\" + ipAddress + @"rootcimv2", connectoptions);
  14.  
  15. // WMI query
  16. var query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");
  17.  
  18. using (var searcher = new ManagementObjectSearcher(scope, query))
  19. {
  20. foreach (ManagementObject process in searcher.Get()) // this is the fixed line
  21. {
  22. process.InvokeMethod("Terminate", null);
  23. }
  24. }
  25.  
  26. public void DeleteDirectory(string target_dir)
  27. {
  28. string[] files = Directory.GetFiles(target_dir);
  29. string[] dirs = Directory.GetDirectories(target_dir);
  30. List<Process> lstProcs = new List<Process>();
  31.  
  32. foreach (string file in files)
  33. {
  34. File.SetAttributes(file, FileAttributes.Normal);
  35. lstProcs = ProcessHandler.WhoIsLocking(file);
  36. if (lstProcs.Count == 0)
  37. File.Delete(file);
  38. else // deal with the file lock
  39. {
  40. foreach (Process p in lstProcs)
  41. {
  42. if (p.MachineName == ".")
  43. ProcessHandler.localProcessKill(p.ProcessName);
  44. else
  45. ProcessHandler.remoteProcessKill(p.MachineName, txtUserName.Text, txtPassword.Password, p.ProcessName);
  46. }
  47. File.Delete(file);
  48. }
  49. }
  50.  
  51. foreach (string dir in dirs)
  52. {
  53. DeleteDirectory(dir);
  54. }
  55.  
  56. //ProcessStartInfo psi = new ProcessStartInfo();
  57. //psi.Arguments = "/C choice /C Y /N /D Y /T 1 & Del " + target_dir;
  58. //psi.WindowStyle = ProcessWindowStyle.Hidden;
  59. //psi.CreateNoWindow = true;
  60. //psi.FileName = "cmd.exe";
  61. //Process.Start(psi);
  62.  
  63. //ProcessStartInfo psi = new ProcessStartInfo();
  64. //psi.Arguments = "/C RMDIR /S / " + target_dir;
  65. //psi.WindowStyle = ProcessWindowStyle.Hidden;
  66. //psi.CreateNoWindow = true;
  67. //psi.FileName = "cmd.exe";
  68. //Process.Start(psi);
  69.  
  70. // This is where the failure occurs
  71. //FileSystem.DeleteDirectory(target_dir, DeleteDirectoryOption.DeleteAllContents);
  72. Directory.Delete(target_dir, false);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement