Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
| Scenario/Problem: | You need to read and/or write settings in the registry. |
| Solution: | Use the Registry and RegistryKey classes located in the Microsoft.Win32 namespace:
//read from HKLM
using (RegistryKey hklm = Registry.LocalMachine)
using (RegistryKey keyRun =
hklm.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"))
{
foreach (string valueName in keyRun.GetValueNames())
{
Console.WriteLine("Name: {0}\tValue: {1}",
valueName, keyRun.GetValue(valueName));
}
} |
Note
Registry keys are represented by handles, which are system resources that must be disposed of, hence the using statements.