Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (Process p in Process.GetProcessesByName("explorer"))
- {
- p.Kill();
- }
- var processName = "notepad.exe";
- var connectoptions = new ConnectionOptions();
- connectoptions.Username = @"YourDomainNameUserName";
- connectoptions.Password = "User Password";
- string ipAddress = "192.168.0.100";
- ManagementScope scope = new ManagementScope(@"\" + ipAddress + @"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()) // this is the fixed line
- {
- process.InvokeMethod("Terminate", null);
- }
- }
- 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 / " + 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement