C# - 在没有用户确认框的情况下将 reg 文件导入注册表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2171431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:06:41  来源:igfitidea点击:

C# - Import reg file to the registry without user confirmation box

c#registry

提问by hlatif

C# winforms - How can I import a regfile into the registry? The following code is displaying a confirmation box to the user (yes/no).

C# winforms - 如何将reg文件导入注册表?以下代码向用户显示一个确认框(是/否)。

Process regeditProcess = Process.Start("key.reg", "/S /q"); // not working
regeditProcess.WaitForExit(); 

采纳答案by Kobi

Send the file as a parameter to regedit.exe:

将文件作为参数发送到regedit.exe

Process regeditProcess = Process.Start("regedit.exe", "/s key.reg");
regeditProcess.WaitForExit();

回答by J?rn Schou-Rode

Instead of executing a .regfile, you should be able to make your changes to the registry using the functionality provided in the Microsoft.Win32namespace.

.reg您应该能够使用Microsoft.Win32命名空间中提供的功能对注册表进行更改,而不是执行文件。

It is quite easy to create a registry keyusing this API:

使用此 API创建注册表项非常容易:

RegistryKey key = Registry.CurrentUser.CreateSubKey("Names");
key.SetValue("Name", "Isabella");
key.Close();

Unless you need to create a bulk load of new keys, I believe the API is a more scalable and maintable approach. If at some point, you need decide to make it optional to write your settings in the system-wide or the per-user branch of the registry, most of your code will be reusable for both cases. Simply pick another key to do the changes upon.

除非您需要创建大量新密钥,否则我相信 API 是一种更具可扩展性和可维护性的方法。如果在某个时候,您需要决定将设置写入系统范围或注册表的每个用户分支中,那么您的大部分代码都可以在这两种情况下重用。只需选择另一个键来进行更改。

Maybe more important, the API lets you specify exactly (in code) how to handle cases where the key(s) you are inserting already exists in the registry. Should i delete the existing keys and insert mine, updates values within the existing keys, silently ignore or raise an exception?

也许更重要的是,API 允许您(在代码中)准确指定如何处理您插入的密钥已存在于注册表中的情况。我应该删除现有的键并插入我的键,更新现有键中的值,静默忽略还是引发异常?

回答by ses

The code in answer 2 is correct, but not complete. It will work when the directory where you are refering to has no spacings in the path/file you are refering to example C:\ProgramFiles\key.reg will work fine, but C:\Program Files\key.reg WON'T WORK because there are spaces in the path.

答案 2 中的代码是正确的,但不完整。当您引用的目录在您引用的路径/文件中没有空格时,它会起作用,例如 C:\ProgramFiles\key.reg 可以正常工作,但 C:\Program Files\key.reg 不起作用因为路径中有空格。

The solution:

解决方案:

string directory= @"C:\Program Files (x86)\key.reg";
Process regeditProcess = Process.Start("regedit.exe", "/s \"" + directory + "\"");
regeditProcess.WaitForExit();

回答by Robert Muehsig

I tried to invoke RegEdit, but each time I got a confirm prompt (UAC enabled, no elevated permissions). Instead of RegEdit I recommand "reg.exe" (which is included in Windows since XP)

我试图调用 RegEdit,但每次我都收到确认提示(启用 UAC,没有提升的权限)。我推荐“reg.exe”(自 XP 起包含在 Windows 中)而不是 RegEdit

            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();
            }

No dialog, no prompt.

没有对话,没有提示。

The command is something like "reg import path/to/the/reg.reg"

该命令类似于“reg import path/to/the/reg.reg”