Tuesday, May 3, 2011

Changing Registry Settings

I recently wanted to change a registry setting programmatically. This is really straight forward actually...
var key = Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced");
key.SetValue("Hidden", 1);

Ah ha! Not quite as easy as I thought - this resulted in an exception

System.UnauthorizedAccessException was unhandled. Message="Cannot write to the registry key."

Luckily there is an overload for OpenSubKey that takes a boolean parameter called writable. So the following code works like a charm.
var key = Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",
true);
key.SetValue("Hidden", 1);

No comments:

Post a Comment