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

前端 未结 4 554
情深已故
情深已故 2020-12-23 17:49

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

         


        
相关标签:
4条回答
  • 2020-12-23 18:30

    Send the file as a parameter to regedit.exe:

    Process regeditProcess = Process.Start("regedit.exe", "/s key.reg");
    regeditProcess.WaitForExit();
    
    0 讨论(0)
  • 2020-12-23 18:38

    Instead of executing a .reg file, you should be able to make your changes to the registry using the functionality provided in the Microsoft.Win32 namespace.

    It is quite easy to create a registry key using this 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.

    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?

    0 讨论(0)
  • 2020-12-23 18:40

    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.

    The solution:

    string directory= @"C:\Program Files (x86)\key.reg";
    Process regeditProcess = Process.Start("regedit.exe", "/s \"" + directory + "\"");
    regeditProcess.WaitForExit();
    
    0 讨论(0)
  • 2020-12-23 18:43

    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)

                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"

    0 讨论(0)
提交回复
热议问题