Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void localProcessKill(string processName)
- {
- foreach (Process p in Process.GetProcessesByName(processName))
- {
- p.Kill();
- }
- }
- public static void remoteProcessKill(string computerName, string fullUserName, string pword, string processName)
- {
- var connectoptions = new ConnectionOptions();
- connectoptions.Username = fullUserName; // @"YourDomainNameUserName";
- connectoptions.Password = pword;
- ManagementScope scope = new ManagementScope(@"\" + computerName + @"rootcimv2", connectoptions);
- // WMI query
- var query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");
- using (var searcher = new ManagementObjectSearcher(scope, query))
- {
- foreach (ManagementObject process in searcher.Get())
- {
- process.InvokeMethod("Terminate", null);
- process.Dispose();
- }
- }
- }
- public void DeleteDirectory(string target_dir)
- {
- string[] files = Directory.GetFiles(target_dir);
- string[] dirs = Directory.GetDirectories(target_dir);
- List<Process> lstProcs = new List<Process>();
- foreach (string file in files)
- {
- File.SetAttributes(file, FileAttributes.Normal);
- lstProcs = ProcessHandler.WhoIsLocking(file);
- if (lstProcs.Count == 0)
- File.Delete(file);
- else // deal with the file lock
- {
- foreach (Process p in lstProcs)
- {
- if (p.MachineName == ".")
- ProcessHandler.localProcessKill(p.ProcessName);
- else
- ProcessHandler.remoteProcessKill(p.MachineName, txtUserName.Text, txtPassword.Password, p.ProcessName);
- }
- File.Delete(file);
- }
- }
- foreach (string dir in dirs)
- {
- DeleteDirectory(dir);
- }
- //ProcessStartInfo psi = new ProcessStartInfo();
- //psi.Arguments = "/C choice /C Y /N /D Y /T 1 & Del " + target_dir;
- //psi.WindowStyle = ProcessWindowStyle.Hidden;
- //psi.CreateNoWindow = true;
- //psi.FileName = "cmd.exe";
- //Process.Start(psi);
- //ProcessStartInfo psi = new ProcessStartInfo();
- //psi.Arguments = "/C RMDIR /S /Q " + target_dir;
- //psi.WindowStyle = ProcessWindowStyle.Hidden;
- //psi.CreateNoWindow = true;
- //psi.FileName = "cmd.exe";
- //Process.Start(psi);
- // This is where the failure occurs
- //FileSystem.DeleteDirectory(target_dir, DeleteDirectoryOption.DeleteAllContents);
- Directory.Delete(target_dir, false);
- }
- [DllImport("kernel32.dll")]
- public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);
- public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;
- //Usage:
- MoveFileEx(fileName, null, MOVEFILE_DELAY_UNTIL_REBOOT);
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
- LogonUser(userName, domainName, password,
- LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
- out safeTokenHandle);
- try
- {
- using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
- {
- using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
- {
- foreach (Computer pc in selectedList) // selectedList is an ObservableCollection<Computer>
- {
- string newDir = "//" + pc.Name + txtExtension.Text; // the textbox has /C$/APP_A_DIR in it
- if (Directory.Exists(newDir))
- {
- DeleteDirectory(newDir); // <-- this is where the exception happens
- }
- }
- }
- }
- }
- catch (IOException ex)
- {
- 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?";
- MessageBoxResult result = MessageBox.Show(msg, "Reboot File Server?", MessageBoxButton.OKCancel);
- if (result == MessageBoxResult.OK)
- {
- var psi = new ProcessStartInfo("shutdown","/s /t 0");
- psi.CreateNoWindow = true;
- psi.UseShellExecute = false;
- Process.Start(psi);
- }
- else
- {
- MessageBox.Show("Copying files...");
- FileSystem.CopyDirectory(sourcePath, newDir);
- MessageBox.Show("Completed!");
- }
- }
- catch (IOException ex)
- {
- if (ex.Message.Contains("The process cannot access the file") &&
- ex.Message.Contains("because it is being used by another process") )
- {
- MessageBox.Show("Copying files...");
- FileSystem.CopyDirectory(sourcePath, newDir);
- MessageBox.Show("Completed!");
- }
- else
- {
- string err = "Issue when performing file copy: " + ex.Message;
- MessageBox.Show(err);
- }
- }
Add Comment
Please, Sign In to add comment