private static void UseRestartManager(string path, ITaskHandler handler) { using (var restartManager = new WindowsRestartManager()) { string[] apps = null; handler.RunTask(new SimpleTask(Resources.RestartManagerSearch, () => { restartManager.RegisterResources(FileUtils.GetFilesRecursive(path)); apps = restartManager.ListApps(); })); if (apps.Length != 0) { string appsList = string.Join(Environment.NewLine, apps); if (handler.Ask(Resources.FilesInUse + @" " + Resources.FilesInUseAskClose + Environment.NewLine + appsList, defaultAnswer: false, alternateMessage: Resources.FilesInUse + @" " + Resources.FilesInUseInform + Environment.NewLine + appsList)) { restartManager.ShutdownApps(handler); } else { throw new OperationCanceledException(); } } } }
/// <summary> /// Uses <see cref="WindowsRestartManager"/> to close any applications that have open references to the specified <paramref name="files"/> if possible and removes read-only attributes. /// </summary> /// <remarks>Closed applications will be restarted by <see cref="Dispose"/>.</remarks> protected void UnlockFiles(IEnumerable <string> files) { if (WindowsUtils.IsWindows) { var fileArray = files.ToArray(); if (fileArray.Length == 0) { return; } if (WindowsUtils.IsWindowsVista) { if (_restartManager == null) { _restartManager = new WindowsRestartManager(); } _restartManager.RegisterResources(fileArray); if (_restartManager.ListApps(Handler).Length == 0) { NoRestart = true; } _restartManager.ShutdownApps(Handler); } foreach (string path in fileArray) { new FileInfo(path).IsReadOnly = false; } } }
private bool RemoveInner(string path, ITaskHandler handler, bool purge = false) { if (path == Locations.InstallBase && WindowsUtils.IsWindows) { Log.Warn(Resources.NoStoreSelfRemove); return(false); } if (WindowsUtils.IsWindowsVista) { try { using var restartManager = new WindowsRestartManager(); // Look for handles to well-known executable types in the top-level directory. // Searching for all file types and/or in subdirectories takes too long. restartManager.RegisterResources(Directory.GetFiles(path, "*.exe")); restartManager.RegisterResources(Directory.GetFiles(path, "*.dll")); string[] apps = restartManager.ListApps(handler.CancellationToken); if (apps.Length != 0) { if (handler.Verbosity != Verbosity.Batch || !purge) { string appsList = string.Join(Environment.NewLine, apps); if (!handler.Ask(Resources.FilesInUse + @" " + Resources.FilesInUseAskClose + Environment.NewLine + appsList, defaultAnswer: false, alternateMessage: Resources.FilesInUse + @" " + Resources.FilesInUseInform + Environment.NewLine + appsList)) { return(false); } } restartManager.ShutdownApps(handler); } } #region Error handling catch (Exception ex) when(ex is IOException or UnauthorizedAccessException or TimeoutException) { Log.Warn(ex); return(false); } catch (Win32Exception ex) { Log.Error(ex); return(false); } #endregion } DisableWriteProtection(path); string tempDir = System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName()); Directory.Move(path, tempDir); Directory.Delete(tempDir, recursive: true); return(true); }
/// <summary> /// Closes any open <see cref="WindowsRestartManager"/> sessions. /// </summary> public void RestartManagerFinish() { if (_restartManager == null) { return; } _restartManager.Dispose(); _restartManager = null; }
private bool RemoveInner(string path, ITaskHandler handler, bool allowAutoShutdown = false) { if (FileUtils.PathEquals(path, Locations.InstallBase)) { Log.Warn(Resources.NoStoreSelfRemove); return(false); } if (WindowsUtils.IsWindowsVista) { try { using var restartManager = new WindowsRestartManager(); // Look for binaries in top-level directory (handling other file types or subdirectories would be too slow) restartManager.RegisterResources(Directory.GetFiles(path, "*.exe").Concat(Directory.GetFiles(path, "*.dll"))); string[] apps = restartManager.ListApps(handler.CancellationToken); if (apps.Length != 0) { string appsList = string.Join(Environment.NewLine, apps); if (handler.Ask(Resources.FilesInUse + " " + Resources.FilesInUseAskClose + Environment.NewLine + appsList, defaultAnswer: allowAutoShutdown)) { restartManager.ShutdownApps(handler); } else { return(false); } } } #region Error handling catch (Exception ex) when(ex is IOException or UnauthorizedAccessException or TimeoutException) { Log.Warn(string.Format(Resources.FailedToUnlockFiles, path), ex); return(false); } catch (Win32Exception ex) { Log.Error("Problem using Windows Restart Manager", ex); return(false); } #endregion } DisableWriteProtection(path); string tempDir = System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName()); Directory.Move(path, tempDir); Directory.Delete(tempDir, recursive: true); return(true); }
/// <summary> /// Ensures that there are no applications running with open file handles in <paramref name="path"/>. /// </summary> /// <returns><c>true</c> if there are no longer any open file handles; <c>false</c> if this could not be ensured.</returns> private static bool UseRestartManager(string path, ITaskHandler handler) { try { using var restartManager = new WindowsRestartManager(); // Look for handles to well-known executable types in the top-level directory // Searching for all file types and/or in subdirectories takes too long restartManager.RegisterResources(Directory.GetFiles(path, "*.exe")); restartManager.RegisterResources(Directory.GetFiles(path, "*.dll")); var apps = restartManager.ListApps(handler); if (apps.Length != 0) { string appsList = string.Join(Environment.NewLine, apps); if (handler.Ask(Resources.FilesInUse + @" " + Resources.FilesInUseAskClose + Environment.NewLine + appsList, defaultAnswer: false, alternateMessage: Resources.FilesInUse + @" " + Resources.FilesInUseInform + Environment.NewLine + appsList)) { restartManager.ShutdownApps(handler); } else { return(false); } } return(true); } #region Error handling catch (IOException ex) { Log.Warn(ex); return(false); } catch (UnauthorizedAccessException ex) { Log.Warn(ex); return(false); } catch (TimeoutException ex) { Log.Warn(ex); return(false); } catch (Win32Exception ex) { Log.Error(ex); return(false); } #endregion }
/// <summary> /// Uses the <see cref="WindowsRestartManager"/> to shut down applications holding references to files we want to update. /// </summary> public void RestartManagerShutdown() { if (!WindowsUtils.IsWindowsVista) { return; } _restartManager = new WindowsRestartManager(); try { _restartManager.RegisterResources(GetFilesToWrite()); _restartManager.RegisterResources(GetFilesToDelete()); _restartManager.ShutdownApps(new SilentTaskHandler()); } #region Error handling catch { _restartManager.Dispose(); throw; } #endregion }