balalakshmi
balalakshmi

Reputation: 4216

What is the risk of updating Registry settings programatically?

I am trying to change the proxy settings to Enabled/Disabled for Internet Explorer programatically using C#. This to toggele between web sites that would require proxy or not.

My Code:

string key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";

RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(key, true);
RegKey.SetValue("ProxyEnable", 1);

Most articles I referred to for updating the registry indicate there is a risk of doing so.

I want to know

  1. What are the risks involved in executing the above code

  2. Is there any mitigation I can do to cover the risks. For example, taking a registry backup for the particular key.

Upvotes: 1

Views: 1106

Answers (2)

Frederik.L
Frederik.L

Reputation: 5620

The registry is an environment that is controlled by the OS and may be subject of unexpectable updates from it. I think that the main risk is to be unable to know what really happens with your data (for example : if Windows thinks it is a good idea to reset or overwrite your values, you lose it all).

Also, like someone said in a comment to another answer, invalid entries may lead to data loss, especially if your data exceeds the maximum length allowed by the registry.

If you need to modify the registry, you may test if the key do exist and keep trace of original values before overwriting. Of course, you need to be sure that altering the key won't have unexpected effects.

Upvotes: 1

Stefan Steinegger
Stefan Steinegger

Reputation: 64638

The risk of changing the registry is simply that you can damage the system. If you really want to run it on a customers machine:

  • Read all the documentation you can find about the registry entries you change
  • Take into account that different windows versions and different product versions may have different registry entries
  • Ask someone who ever changed these values
  • Make sure your application will have the permission to do this (say: run as administrator, which should not be done for regular usage. Only for instance in an installer).
  • Make sure you can uninstall the changes

Upvotes: 1

Related Questions