指定应用 在 控制面板或者 设置的安装应用 置灰或隐藏卸载按钮
桌面应用的安装卸载功能,会有需求支持在控制面板卸载指定的应用,或者在设置-应用-安装的应用显示卸载的应用。如下图所示。
最近有产品提出来需求,我们的灵犀助手不希望用户去卸载它,也就是说要屏蔽掉用户 卸载应用的入口。
在windows的注册表下,可以指定某个应用在安装列表显示:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
指定某个应用下的安装现象,需要在上述的注册表项上,添加一个ID项目:如:{2f39546c-7b63-4ec2-99bd-e434b8c75ab6}
解决方案:
在指定应用项,“新建”--"DWORD(32位)值"--修改名称:“Noremove”--编辑项:1
C#实现置灰卸载按钮的代码:
using RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", true); using RegistryKey software = key?.CreateSubKey("{2f39546c-7b63-4ec2-99bd-e434b8c75ab6}"); // software?.SetValue("DisplayName", IniUtils.ReadValue(appInfoFile, sectionName, "AppDisplayName")); software?.SetValue("DisplayName", "灵犀助手"); software?.SetValue("DisplayVersion", IniUtils.ReadValue(appInfoFile, sectionName, "AppVersion")); software?.SetValue("Publisher", IniUtils.ReadValue(appInfoFile, sectionName, "AppPublisher")); software?.SetValue("NoRemove","1",RegistryValueKind.DWord); //安装目录 software?.SetValue("InstallLocation", CustomText.DefaultInstallPath); software?.SetValue("InstallSource", CustomText.DefaultInstallPath); software?.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd")); software?.SetValue("UninstallString", Path.Combine(CustomText.DefaultInstallPath, "LinseerCopilot_unins.exe"));
效果如下图:
设置的应用列表显示:
控制面板-卸载程序列表显示: