private void CacheCacheByCmd(BackgroundWorker worker) { worker.ReportProgress(0); var commands = ToCleanUpManager.GetCleanCacheCommands(); List <string> toCleaningFiles = new List <string>(); foreach (var keyValuePair in GetCacheFiles()) { toCleaningFiles.AddRange(keyValuePair.Value); } var totalSize = GetCurrentCacheSize(); int index = 0; foreach (var command in commands) { index++; var currentOperationError = string.Empty; var currentOperationOutput = string.Empty; try { var currentOperationDetail = $"执行{command},可能需要几分钟,请等待"; worker.ReportProgress(-1, new ProgressChangedContent() { OperationDetail = currentOperationDetail, }); if (command.Contains(@"C:\Windows\Installer\$PatchCache$")) { ExecuteCmdHelper.ExecuteCmd("Net Start msiserver /Y"); ExecuteCmdHelper.ExecuteCmd("Net Stop msiserver /Y"); ExecuteCmdHelper.ExecuteCmd(@"Reg Add HKLM\Software\Policies\Microsoft\Windows\Installer /v MaxPatchCacheSize /t REG_DWORD /d 0 /f"); ExecuteCmdHelper.ExecuteCmd(command); ExecuteCmdHelper.ExecuteCmd("Net Start msiserver /Y"); ExecuteCmdHelper.ExecuteCmd("Net Stop msiserver /Y"); ExecuteCmdHelper.ExecuteCmd(@"Reg Add HKLM\Software\Policies\Microsoft\Windows\Installer /v MaxPatchCacheSize /t REG_DWORD /d 10 /f"); ExecuteCmdHelper.ExecuteCmd("Net Start msiserver /Y"); } var executeCmdResult = ExecuteCmdHelper.ExecuteCmd(command); //currentOperationOutput = executeCmdResult.Replace(ExecuteCmdHelper.ExistStr, string.Empty).Trim(); var cleanedSize = totalSize - GetCurrentCacheSize(); currentOperationOutput += $"执行{command},共删除{UnitConverter.ConvertSize(cleanedSize)}"; } catch (Exception e) { currentOperationError = $"{command}执行失败"; } finally { worker.ReportProgress(Convert.ToInt32(Convert.ToDouble(index) / Convert.ToDouble(commands.Count)), new ProgressChangedContent() { OperationOutput = currentOperationOutput, OperationError = currentOperationError }); } } }
/// <summary> /// 时间较长,额外处理 /// </summary> private async void ExecuteDelayCommands() { var cleanDelayedCommands = ToCleanUpManager.GetCleanDelayedCommands(); foreach (var cleanDelayedCommand in cleanDelayedCommands) { var totalSize = GetCurrentCacheSize(); await Task.Run(() => { ExecuteCmdHelper.ExecuteCmd(cleanDelayedCommand); }); var cleanedSize = totalSize - GetCurrentCacheSize(); OutputText += $"执行{cleanDelayedCommand},共删除{UnitConverter.ConvertSize(cleanedSize)}" + "\r\n"; } }