C#winforms - 如何将reg文件导入注册表?以下代码向用户显示确认框(是/否).
Process regeditProcess = Process.Start("key.reg", "/S /q"); // not working
regeditProcess.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
Kob*_*obi 46
将文件作为参数发送到regedit.exe:
Process regeditProcess = Process.Start("regedit.exe", "/s key.reg");
regeditProcess.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
资料来源:http://www.mycsharpcorner.com/Post.aspx? postIDID = 29
小智 13
答案2中的代码是正确的,但不完整.当您引用的目录在您引用的路径/文件中没有间距时,它将起作用示例C:\ ProgramFiles\key.reg将正常工作,但C:\ Program Files\key.reg将无法工作因为路径中有空格.
解决方案:
string directory= @"C:\Program Files (x86)\key.reg";
Process regeditProcess = Process.Start("regedit.exe", "/s \"" + directory + "\"");
regeditProcess.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
我试图调用RegEdit,但每次我收到确认提示(启用UAC,没有提升权限).而不是RegEdit我推荐"reg.exe"(自XP以来包含在Windows中)
Process proc = new Process();
try
{
proc.StartInfo.FileName = "reg.exe";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
string command = "import " + path;
proc.StartInfo.Arguments = command;
proc.Start();
proc.WaitForExit();
}
catch (System.Exception)
{
proc.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
没有对话,没有提示.
该命令类似于"reg import path/to/the/reg.reg"
.reg您应该能够使用Microsoft.Win32命名空间中提供的功能对注册表进行更改,而不是执行文件.
使用此API 创建注册表项非常容易:
RegistryKey key = Registry.CurrentUser.CreateSubKey("Names");
key.SetValue("Name", "Isabella");
key.Close();
Run Code Online (Sandbox Code Playgroud)
除非您需要创建大量新密钥,否则我认为API是一种更具可扩展性和可维护性的方法.如果在某些时候,您需要决定使其在注册表的系统范围或每用户分支中编写您的设置是可选的,那么大多数代码都可以在两种情况下重复使用.只需选择另一个键即可进行更改.
也许更重要的是,API允许您准确指定(在代码中)如何处理您要插入的密钥已经存在于注册表中的情况.我应该删除现有密钥并插入我的密钥,更新现有密钥中的值,静默忽略或引发异常吗?
| 归档时间: |
|
| 查看次数: |
29857 次 |
| 最近记录: |