Guest User

Untitled

a guest
Nov 20th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. public static void localProcessKill(string processName)
  2. {
  3. foreach (Process p in Process.GetProcessesByName(processName))
  4. {
  5. p.Kill();
  6. }
  7. }
  8.  
  9. public static void remoteProcessKill(string computerName, string fullUserName, string pword, string processName)
  10. {
  11. var connectoptions = new ConnectionOptions();
  12. connectoptions.Username = fullUserName; // @"YourDomainNameUserName";
  13. connectoptions.Password = pword;
  14.  
  15. ManagementScope scope = new ManagementScope(@"\" + computerName + @"rootcimv2", connectoptions);
  16.  
  17. // WMI query
  18. var query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");
  19.  
  20. using (var searcher = new ManagementObjectSearcher(scope, query))
  21. {
  22. foreach (ManagementObject process in searcher.Get())
  23. {
  24. process.InvokeMethod("Terminate", null);
  25. process.Dispose();
  26. }
  27. }
  28. }
  29.  
  30. public void DeleteDirectory(string target_dir)
  31. {
  32. string[] files = Directory.GetFiles(target_dir);
  33. string[] dirs = Directory.GetDirectories(target_dir);
  34. List<Process> lstProcs = new List<Process>();
  35.  
  36. foreach (string file in files)
  37. {
  38. File.SetAttributes(file, FileAttributes.Normal);
  39. lstProcs = ProcessHandler.WhoIsLocking(file);
  40. if (lstProcs.Count == 0)
  41. File.Delete(file);
  42. else // deal with the file lock
  43. {
  44. foreach (Process p in lstProcs)
  45. {
  46. if (p.MachineName == ".")
  47. ProcessHandler.localProcessKill(p.ProcessName);
  48. else
  49. ProcessHandler.remoteProcessKill(p.MachineName, txtUserName.Text, txtPassword.Password, p.ProcessName);
  50. }
  51. File.Delete(file);
  52. }
  53. }
  54.  
  55. foreach (string dir in dirs)
  56. {
  57. DeleteDirectory(dir);
  58. }
  59.  
  60. //ProcessStartInfo psi = new ProcessStartInfo();
  61. //psi.Arguments = "/C choice /C Y /N /D Y /T 1 & Del " + target_dir;
  62. //psi.WindowStyle = ProcessWindowStyle.Hidden;
  63. //psi.CreateNoWindow = true;
  64. //psi.FileName = "cmd.exe";
  65. //Process.Start(psi);
  66.  
  67. //ProcessStartInfo psi = new ProcessStartInfo();
  68. //psi.Arguments = "/C RMDIR /S /Q " + target_dir;
  69. //psi.WindowStyle = ProcessWindowStyle.Hidden;
  70. //psi.CreateNoWindow = true;
  71. //psi.FileName = "cmd.exe";
  72. //Process.Start(psi);
  73.  
  74. // This is where the failure occurs
  75. //FileSystem.DeleteDirectory(target_dir, DeleteDirectoryOption.DeleteAllContents);
  76. Directory.Delete(target_dir, false);
  77. }
  78.  
  79. [DllImport("kernel32.dll")]
  80. public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);
  81.  
  82. public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;
  83.  
  84. //Usage:
  85. MoveFileEx(fileName, null, MOVEFILE_DELAY_UNTIL_REBOOT);
  86.  
  87. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  88. public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
  89.  
  90. LogonUser(userName, domainName, password,
  91. LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
  92. out safeTokenHandle);
  93.  
  94. try
  95. {
  96. using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
  97. {
  98. using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
  99. {
  100. foreach (Computer pc in selectedList) // selectedList is an ObservableCollection<Computer>
  101. {
  102. string newDir = "//" + pc.Name + txtExtension.Text; // the textbox has /C$/APP_A_DIR in it
  103. if (Directory.Exists(newDir))
  104. {
  105. DeleteDirectory(newDir); // <-- this is where the exception happens
  106. }
  107. }
  108. }
  109. }
  110. }
  111. catch (IOException ex)
  112. {
  113. string msg = "There was a file left open, thereby preventing a full deletion of the previous folder, though all contents have been removed. Do you wish to proceed with installation, or reboot the server and begin again, in order to remove and replace the installation directory?";
  114. MessageBoxResult result = MessageBox.Show(msg, "Reboot File Server?", MessageBoxButton.OKCancel);
  115. if (result == MessageBoxResult.OK)
  116. {
  117. var psi = new ProcessStartInfo("shutdown","/s /t 0");
  118. psi.CreateNoWindow = true;
  119. psi.UseShellExecute = false;
  120. Process.Start(psi);
  121. }
  122. else
  123. {
  124. MessageBox.Show("Copying files...");
  125. FileSystem.CopyDirectory(sourcePath, newDir);
  126. MessageBox.Show("Completed!");
  127. }
  128. }
  129.  
  130. catch (IOException ex)
  131. {
  132. if (ex.Message.Contains("The process cannot access the file") &&
  133. ex.Message.Contains("because it is being used by another process") )
  134. {
  135. MessageBox.Show("Copying files...");
  136. FileSystem.CopyDirectory(sourcePath, newDir);
  137. MessageBox.Show("Completed!");
  138. }
  139. else
  140. {
  141. string err = "Issue when performing file copy: " + ex.Message;
  142. MessageBox.Show(err);
  143. }
  144. }
Add Comment
Please, Sign In to add comment