/// <summary> /// Finds a running Windows Explorer instance and causes it restart. /// </summary> public static void RestartExplorer() { if (OperatingSystemVersions.IsSupported(OperatingSystemVersion.WindowsVista)) { using (var rm = new RestartManagerSession()) { rm.RegisterProcess(Process.GetProcessesByName("explorer")); rm.Shutdown(RestartManagerSession.ShutdownType.Normal); rm.Restart(); } } else { Process[] processes = Process.GetProcessesByName("explorer"); foreach (Process process in processes) { process.Kill(); } foreach (Process process in processes) { process.Start(); } } }
public void TestRestartManagerProcessFileMove() { var ReplacementProcessCopy = ReplacementProcess + "2"; File.Copy(ReplacementProcess, ReplacementProcessCopy, true); //Start up the service and TRY to move the file. It should fail Process proc = Process.Start(ReplacementProcess); //We're hoping this will fail, as the process SHOULD be holding onto this guy MyAssert.ThrowsException(() => File.Copy(ReplacementProcessCopy, ReplacementProcess, true)); //Now startup the restart manager and lets hope the process will be restarted. RestartManagerSession manager = new RestartManagerSession(); manager.StartSession(); manager.RegisterResources(new List <string>() { ReplacementProcess }); RM_REBOOT_REASON rebootReason = default(RM_REBOOT_REASON); var processes = RestartManager.RmProcessToNetProcess(manager.GetList(ref rebootReason)); Assert.IsTrue(rebootReason == RM_REBOOT_REASON.RmRebootReasonNone); Assert.IsTrue(processes.Count > 0); //After shutdown, the file should be copyable manager.Shutdown(); ThreadingServices.WaitOnAction(() => File.Copy(ReplacementProcessCopy, ReplacementProcess, true), TimeSpan.FromSeconds(3)); //Now try to restart everything manager.Restart(); manager.EndSession(); }