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

前端 未结 4 560
情深已故
情深已故 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: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?

提交回复
热议问题