如何通过C#或cmd命令将程序添加到控制面板的程序列表中
程序添加到控制面板程序列表实现方案
要实现将自定义程序添加到控制面板「程序和功能」列表中,效果如下:
本质是通过修改Windows对应注册表项实现,以下是两种可直接使用的实现方案:
方法1:CMD命令实现
操作HKEY_LOCAL_MACHINE节点需要管理员权限运行CMD,如果仅对当前用户生效不需要管理员,可以将路径中的HKLM替换为HKCU。
:: 替换变量为你的程序实际信息 set AppGuid={自定义唯一GUID,比如{8A2F6D1E-7B3C-4D5E-8F9A-1B2C3D4E5F6A}} set AppName=测试程序 set AppPublisher=个人开发者 set AppVersion=1.0.0 set InstallPath=D:\TestApp set UninstallExe=D:\TestApp\uninstall.exe set AppIcon=D:\TestApp\TestApp.exe,0 :: 写入注册表 reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%AppGuid%" /v DisplayName /d "%AppName%" /f reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%AppGuid%" /v Publisher /d "%AppPublisher%" /f reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%AppGuid%" /v DisplayVersion /d "%AppVersion%" /f reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%AppGuid%" /v InstallLocation /d "%InstallPath%" /f reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%AppGuid%" /v UninstallString /d "%UninstallExe%" /f reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%AppGuid%" /v DisplayIcon /d "%AppIcon%" /f reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%AppGuid%" /v EstimatedSize /t REG_DWORD /d 20480 /f
参数说明:
EstimatedSize为程序占用大小,单位为KB。
方法2:C#代码实现
操作本地机器注册表节点需要程序以管理员权限运行,示例代码如下:
using Microsoft.Win32; namespace AddProgramToControlPanel { class Program { static void Main(string[] args) { // 程序唯一GUID,固定不要随意修改 string appGuid = "{8A2F6D1E-7B3C-4D5E-8F9A-1B2C3D4E5F6A}"; // 全局路径需要管理员权限,如需仅当前用户生效,替换Registry.LocalMachine为Registry.CurrentUser string regPath = $@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{appGuid}"; using (RegistryKey regKey = Registry.LocalMachine.CreateSubKey(regPath)) { if (regKey != null) { regKey.SetValue("DisplayName", "测试程序"); regKey.SetValue("Publisher", "个人开发者"); regKey.SetValue("DisplayVersion", "1.0.0"); regKey.SetValue("InstallLocation", @"D:\TestApp"); regKey.SetValue("UninstallString", @"D:\TestApp\uninstall.exe"); regKey.SetValue("DisplayIcon", @"D:\TestApp\TestApp.exe,0"); regKey.SetValue("EstimatedSize", 20480, RegistryValueKind.DWord); // 隐藏修改、修复按钮,不需要可以删除这两行 regKey.SetValue("NoModify", 1, RegistryValueKind.DWord); regKey.SetValue("NoRepair", 1, RegistryValueKind.DWord); } } } } }
操作完成后刷新控制面板的程序列表即可看到新增的条目。
内容的提问来源于stack exchange,提问作者Ali Aliyev
相关产品推荐
相关产品推荐

