Portable Tool for an Ideal Windows Setup
C# activatorbloatware-removalcsharpdebloatdebloaterdisable-servicesmonitoring-systemportableprivacy-toolsregistry-tweakssystem-tuningsystem-utilitytelemetrytweakerwindows-10windows-11windows-debloatwindows-defenderwindows-optimizationwindows-tweaks
unify file directory resolution and remove duplicate entries
Greedeksbe2ce72484d2
diff --git a/.Source/GTweak/Utilities/Controls/PathLocator.cs b/.Source/GTweak/Utilities/Controls/PathLocator.csindex b979ffec..273b585c 100644--- a/.Source/GTweak/Utilities/Controls/PathLocator.cs+++ b/.Source/GTweak/Utilities/Controls/PathLocator.cs@@ -1,4 +1,4 @@-using System;+using System;using System.IO;using System.Linq;@@ -20,7 +20,7 @@ namespace GTweak.Utilities.Controlsinternal static readonly string WindowsOld = Path.Combine(SystemDrive, "Windows.old");- internal static readonly string Tasks = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "System32", "Tasks");+ internal static readonly string Tasks = Path.Combine(Environment.SystemDirectory, "Tasks");}internal static class Executable@@ -37,6 +37,43 @@ namespace GTweak.Utilities.Controlsreturn searchDir.Select(dir => Path.Combine(dir, name)).FirstOrDefault(File.Exists);}+ internal static (string Normal, string Block) FindWindowsUpdateExe(string normalName, string blockName)+ {+ static string FindFile(string fileName)+ {+ string uusPath = Path.Combine(Folders.SystemDrive, "Windows", "UUS", "amd64", fileName);+ string system32Path = Path.Combine(Environment.SystemDirectory, fileName);++ if (File.Exists(uusPath))+ {+ return uusPath;+ }++ if (File.Exists(system32Path))+ {+ return system32Path;+ }++ return null;+ }++ string normalPath = FindFile(normalName);+ string blockPath = FindFile(blockName);++ if (normalPath != null)+ {+ return (normalPath, Path.Combine(Path.GetDirectoryName(normalPath), blockName));+ }++ if (blockPath != null)+ {+ return (Path.Combine(Path.GetDirectoryName(blockPath), normalName), blockPath);+ }++ return (string.Empty, string.Empty);+ }++internal static readonly string CommandShell = FindExecutablePath("cmd.exe");internal static readonly string PowerShell = FindExecutablePath("pwsh.exe") ?? FindExecutablePath("powershell.exe");@@ -50,6 +87,20 @@ namespace GTweak.Utilities.Controlsinternal static readonly string OneDrive = FindExecutablePath("onedrivesetup.exe");internal static readonly string NSudo = Path.Combine(Folders.DefenderBackup, "NSudoLC.exe");++ internal static (string Normal, string Block) UsoClient =>+ (+ Path.Combine(Environment.SystemDirectory, "usoclient.exe"),+ Path.Combine(Environment.SystemDirectory, "BlockUOrchestrator-GTweak.exe")+ );++ internal static (string Normal, string Block) WorkerCore => FindWindowsUpdateExe("MoUsoCoreWorker.exe", "BlockUpdate-GTweak.exe");++ internal static (string Normal, string Block) WuauClient => FindWindowsUpdateExe("wuaucltcore.exe", "BlockUpdateCore-GTweak.exe");++ internal static (string Normal, string Block) WaaSMedic => FindWindowsUpdateExe("WaaSMedicAgent.exe", "BlockUpdateAgent-GTweak.exe");++ internal static readonly string MpCmdRun = Path.Combine(Folders.SystemDrive, "Program Files", "Windows Defender", "MpCmdRun.exe");}internal static class Filesdiff --git a/.Source/GTweak/Utilities/Managers/FirewallManager.cs b/.Source/GTweak/Utilities/Managers/FirewallManager.csindex cc3f4a90..d1d6307c 100644--- a/.Source/GTweak/Utilities/Managers/FirewallManager.cs+++ b/.Source/GTweak/Utilities/Managers/FirewallManager.cs@@ -1,6 +1,5 @@using System;using System.Collections.Generic;-using System.IO;using System.Threading.Tasks;using GTweak.Utilities.Controls;using NetFwTypeLib;@@ -16,14 +15,6 @@ namespace GTweak.Utilities.Managers["WDefender"] = @"GTweak - Windows Defender blocking"};- private static readonly SortedList<string, string> _сollectExecutablePaths = new SortedList<string, string>- {- ["MoUso"] = File.Exists($@"{PathLocator.Folders.SystemDrive}Windows\UUS\amd64\MoUsoCoreWorker.exe")- ? $@"{PathLocator.Folders.SystemDrive}Windows\UUS\amd64\MoUsoCoreWorker.exe" : $@"{PathLocator.Folders.SystemDrive}Windows\System32\MoUsoCoreWorker.exe",- ["Uso"] = string.Concat(PathLocator.Folders.SystemDrive, @"Windows\System32\usoclient.exe"),- ["WD"] = string.Concat(PathLocator.Folders.SystemDrive, @"Program Files\Windows Defender\MpCmdRun.exe"),- };-private static void RemoveAllRulesByName(string nameRule){INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));@@ -63,13 +54,14 @@ namespace GTweak.Utilities.Managers{Parallel.ForEach(new[]{- (_сollectExecutablePaths["MoUso"], _nameRules["Update"]),- (_сollectExecutablePaths["Uso"], $"{_nameRules["Update"]} (Update Orchestrator)")+ (PathLocator.Executable.WorkerCore.Normal, _nameRules["Update"]),+ (PathLocator.Executable.UsoClient.Normal, $"{_nameRules["Update"]} (Update Orchestrator)")},- executableFiles =>+ executableFile =>{- ChangeRules(isDisabled, executableFiles.Item1, executableFiles.Item2, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN);- ChangeRules(isDisabled, executableFiles.Item1, executableFiles.Item2, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT);+ (string path, string ruleName) = executableFile;+ ChangeRules(isDisabled, path, ruleName, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN);+ ChangeRules(isDisabled, path, ruleName, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT);});}catch { NotificationManager.Show("warn", "warn_firewall_noty").Perform(); }@@ -156,7 +148,7 @@ namespace GTweak.Utilities.Managersprotected static void BlockWDefender(bool isDisabled){- try { Parallel.Invoke(() => { ChangeRules(isDisabled, _сollectExecutablePaths["WD"], _nameRules["WDefender"], NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT, "blocking Windows Defender database updates"); }); }+ try { Parallel.Invoke(() => { ChangeRules(isDisabled, PathLocator.Executable.MpCmdRun, _nameRules["WDefender"], NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT, "blocking Windows Defender database updates"); }); }catch (Exception ex) { ErrorLogging.LogDebug(ex); }}}diff --git a/.Source/GTweak/Utilities/Tweaks/DefenderManager/WindowsDefender.cs b/.Source/GTweak/Utilities/Tweaks/DefenderManager/WindowsDefender.csindex af1057cd..81e8f818 100644--- a/.Source/GTweak/Utilities/Tweaks/DefenderManager/WindowsDefender.cs+++ b/.Source/GTweak/Utilities/Tweaks/DefenderManager/WindowsDefender.cs@@ -32,6 +32,22 @@ namespace GTweak.Utilities.Tweaks.DefenderManager{ "wscsvc", "3" }};+ private static readonly (string Path, string Normal, string Block)[] fileMappings = new (string Path, string Normal, string Block)[]+ {+ (Environment.SystemDirectory, "smartscreen.exe", "BlockSS.exe"),+ (PathLocator.Folders.WindowsDefender, "MsMpEng.exe", "BlockAntimalware.exe"),+ (PathLocator.Folders.WindowsDefender, "MpDefenderCoreService.exe", "BlockAntimalwareCore.exe"),+ (PathLocator.Folders.WindowsDefender, "MpCmdRun.exe", "BlockMpCmdRun.exe"),+ (PathLocator.Folders.WindowsDefender, "MpCopyAccelerator.exe", "BlockMpCopyAccelerator.exe"),+ (PathLocator.Folders.WindowsDefender, "DlpUserAgent.exe", "BlockDlpUserAgent.exe"),+ (PathLocator.Folders.WindowsDefender, "MpDlpCmd.exe", "BlockMpDlpCmd.exe"),+ (PathLocator.Folders.WindowsDefender, "MpDlpService.exe", "BlockMpDlpService.exe"),+ (PathLocator.Folders.WindowsDefender, "mpextms.exe", "Blockmpextms.exe"),+ (PathLocator.Folders.WindowsDefender, "NisSrv.exe", "BlockNisSrv.exe"),+ (PathLocator.Folders.WindowsDefender, "ConfigSecurityPolicy.exe", "BlockConfigSecurityPolicy.exe"),+ (PathLocator.Folders.WindowsDefenderX86, "MpCmdRun.exe", "BlockMpCmdRun.exe"),+ };+internal static void SetProtectionState(bool isDisabled){if (isDisabled)@@ -46,23 +62,9 @@ namespace GTweak.Utilities.Tweaks.DefenderManagerprivate static void Activate(){- foreach (var (filePath, originalFileName) in new (string filePath, string originalFileName)[]+ foreach (var (path, normal, block) in fileMappings){- (Path.Combine(PathLocator.Folders.SystemDrive, "Windows", "System32", "BlockSS.exe"), "smartscreen.exe"),- (Path.Combine(PathLocator.Folders.SystemDrive, "Program Files", "Windows Defender", "BlockAntimalware.exe"), "MsMpEng.exe"),- (Path.Combine(PathLocator.Folders.SystemDrive, "Program Files", "Windows Defender", "BlockAntimalwareCore.exe"), "MpDefenderCoreService.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "BlockMpCmdRun.exe"), "MpCmdRun.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "BlockMpCopyAccelerator.exe"), "MpCopyAccelerator.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "BlockDlpUserAgent.exe"), "DlpUserAgent.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "BlockMpDlpCmd.exe"), "MpDlpCmd.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "BlockMpDlpService.exe"), "MpDlpService.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "Blockmpextms.exe"), "mpextms.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "BlockNisSrv.exe"), "NisSrv.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "BlockConfigSecurityPolicy.exe"), "ConfigSecurityPolicy.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefenderX86, "BlockMpCmdRun.exe"), "MpCmdRun.exe"),- })- {- CommandExecutor.RunCommandAsTrustedInstaller($"{PathLocator.Executable.NSudo} -U:T -P:E -ShowWindowMode:Hide -Wait cmd /c rename \"{filePath}\" {originalFileName}");+ CommandExecutor.RunCommandAsTrustedInstaller($"{PathLocator.Executable.NSudo} -U:T -P:E -ShowWindowMode:Hide -Wait cmd /c rename \"{Path.Combine(path, block)}\" {normal}");}CommandExecutor.RunCommandAsTrustedInstaller($"{PathLocator.Executable.NSudo} -U:T -P:E -ShowWindowMode:Hide -Wait cmd /c for /d %D in (\"{Path.Combine(PathLocator.Folders.SystemDrive, @"ProgramData\Microsoft\Windows Defender\Platform\*")}\") do if exist \"%D\\BlockAntimalware.exe\" ren \"%D\\BlockAntimalware.exe\" MsMpEng.exe");@@ -193,23 +195,9 @@ namespace GTweak.Utilities.Tweaks.DefenderManagerKillProcess(new[] { "smartscreen", "MpDefenderCoreService", "MsMpEng", "SecurityHealthService", "SecurityHealthSystray", "SecurityHealthUI", "wuauserv", "SearchUI", "SecHealthUI", "RuntimeBroker", "msedge", "ssoncom", "usocoreworker" });- foreach (var (filePath, newFileName) in new (string filePath, string newFileName)[]- {- (Path.Combine(PathLocator.Folders.SystemDrive, "Windows", "System32", "smartscreen.exe"), "BlockSS.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "MsMpEng.exe"), "BlockAntimalware.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "MpDefenderCoreService.exe"), "BlockAntimalwareCore.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "MpCmdRun.exe"), "BlockMpCmdRun.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "MpCopyAccelerator.exe"), "BlockMpCopyAccelerator.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "DlpUserAgent.exe"), "BlockDlpUserAgent.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "MpDlpCmd.exe"), "BlockMpDlpCmd.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "MpDlpService.exe"), "BlockMpDlpService.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "mpextms.exe"), "Blockmpextms.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "NisSrv.exe"), "BlockNisSrv.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefender, "ConfigSecurityPolicy.exe"), "BlockConfigSecurityPolicy.exe"),- (Path.Combine(PathLocator.Folders.WindowsDefenderX86, "MpCmdRun.exe"), "BlockMpCmdRun.exe"),- })+ foreach (var (path, normal, block) in fileMappings){- CommandExecutor.RunCommandAsTrustedInstaller($"{PathLocator.Executable.NSudo} -U:T -P:E -ShowWindowMode:Hide -Wait cmd /c rename \"{filePath}\" {newFileName}");+ CommandExecutor.RunCommandAsTrustedInstaller($"{PathLocator.Executable.NSudo} -U:T -P:E -ShowWindowMode:Hide -Wait cmd /c rename \"{Path.Combine(path, normal)}\" {block}");}CommandExecutor.RunCommandAsTrustedInstaller($"{PathLocator.Executable.NSudo} -U:T -P:E -ShowWindowMode:Hide -Wait cmd /c for /d %D in (\"{Path.Combine(PathLocator.Folders.SystemDrive, @"ProgramData\Microsoft\Windows Defender\Platform\*")}\") do if exist \"%D\\MsMpEng.exe\" ren \"%D\\MsMpEng.exe\" BlockAntimalware.exe");diff --git a/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs b/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.csindex 8838ffd7..c3b14214 100644--- a/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs+++ b/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs@@ -3,7 +3,6 @@ using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;-using System.Text.RegularExpressions;using System.Threading;using System.Threading.Tasks;using GTweak.Utilities.Controls;@@ -17,22 +16,7 @@ namespace GTweak.Utilities.Tweaks{internal static Dictionary<string, object> ControlStates = new Dictionary<string, object>();private readonly ControlWriterManager _сontrolWriter = new ControlWriterManager(ControlStates);-- private static readonly Dictionary<string, (string Default, string Blocked)> _updateFilesWin = new Dictionary<string, (string Default, string Blocked)>()- {- { "Uso", ("usoclient.exe", "BlockUOrchestrator-GTweak.exe") },- { "Worker", ("MoUsoCoreWorker.exe", "BlockUpdate-GTweak.exe") },- { "Core", ("wuaucltcore.exe", "BlockUpdateCore-GTweak.exe") },- { "Agent", ("WaaSMedicAgent.exe", "BlockUpdateAgent-GTweak.exe") }- };-- private static string FilesPathUpdate(string program, bool isOldWay = false)- {- bool isUsoClient = _updateFilesWin.TryGetValue("Uso", out var usoFiles) && Regex.IsMatch(program, $"{usoFiles.Default}|{usoFiles.Blocked}", RegexOptions.IgnoreCase);- string basePath = (isOldWay || isUsoClient) ? Path.Combine(PathLocator.Folders.SystemDrive, "Windows", "System32") : Path.Combine(PathLocator.Folders.SystemDrive, "Windows", "UUS", "amd64");-- return Path.Combine(basePath, program);- }+ private readonly (string Normal, string Block)[] _updateFiles = new[] { PathLocator.Executable.UsoClient, PathLocator.Executable.WorkerCore, PathLocator.Executable.WuauClient, PathLocator.Executable.WaaSMedic };internal void AnalyzeAndUpdate(){@@ -46,7 +30,7 @@ namespace GTweak.Utilities.TweaksRegistryHelp.CheckValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\XboxNetApiSvc", "Start", "4") ||RegistryHelp.CheckValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\XblGameSave", "Start", "4");- _сontrolWriter.Button[3] = _updateFilesWin.All(key => File.Exists(FilesPathUpdate(key.Value.Default)) || File.Exists(FilesPathUpdate(key.Value.Default, true)));+ _сontrolWriter.Button[3] = _updateFiles.All(f => File.Exists(f.Normal));_сontrolWriter.Button[4] =RegistryHelp.CheckValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WalletService", "Start", "4") ||@@ -211,27 +195,30 @@ namespace GTweak.Utilities.TweaksBlockWindowsUpdate(isDisabled);ChangeAccessUpdateFolders(isDisabled);- foreach (var key in _updateFilesWin)+ foreach (var (Normal, Block) in _updateFiles){- string currentFileName = isDisabled ? key.Value.Default : key.Value.Blocked;- string targetFileName = isDisabled ? key.Value.Blocked : key.Value.Default;-- string currentFilePath = FilesPathUpdate(currentFileName);- string targetFilePath = FilesPathUpdate(targetFileName);+ string currentFilePath = isDisabled ? Normal : Block;+ string targetFilePath = isDisabled ? Block : Normal;try{+ if (isDisabled && File.Exists(currentFilePath) && File.Exists(targetFilePath))+ {+ CommandExecutor.RunCommand($"/c del /f \"{targetFilePath}\"");+ }+if (isDisabled){- CommandExecutor.RunCommandAsTrustedInstaller($"/c takeown /f \"{currentFilePath}\" & icacls \"{currentFilePath}\" /inheritance:r /remove S-1-5-32-544 S-1-5-11 S-1-5-32-545 S-1-5-18 & icacls \"{currentFilePath}\" /grant {Environment.UserName}:F & rename \"{currentFilePath}\" \"{targetFileName}\"");+ CommandExecutor.RunCommandAsTrustedInstaller($"/c takeown /f \"{currentFilePath}\" & icacls \"{currentFilePath}\" /inheritance:r /remove S-1-5-32-544 S-1-5-11 S-1-5-32-545 S-1-5-18 & icacls \"{currentFilePath}\" /grant {Environment.UserName}:F & rename \"{currentFilePath}\" \"{Path.GetFileName(targetFilePath)}\"");}else{- CommandExecutor.RunCommandAsTrustedInstaller($"/c rename \"{currentFilePath}\" \"{targetFileName}\" & icacls \"{targetFilePath}\" /reset & takeown /f \"{targetFilePath}\" /a & icacls \"{targetFilePath}\" /setowner *S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464");+ CommandExecutor.RunCommandAsTrustedInstaller($"/c rename \"{currentFilePath}\" \"{Path.GetFileName(targetFilePath)}\" & icacls \"{targetFilePath}\" /reset & takeown /f \"{targetFilePath}\" /a & icacls \"{targetFilePath}\" /setowner *S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464");}}catch (Exception ex) { ErrorLogging.LogDebug(ex); }}+RegistryHelp.Write(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Services\wisvc", "Start", isDisabled ? 4 : 3, RegistryValueKind.DWord);RegistryHelp.Write(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Services\DmEnrollmentSvc", "Start", isDisabled ? 4 : 3, RegistryValueKind.DWord);RegistryHelp.Write(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Services\wuauserv", "Start", isDisabled ? 4 : 3, RegistryValueKind.DWord);