Reputation: 14963
How do you insert the value 1
into the property CDInsert
in HKEY_LOCAL_MACHINE\SOFTWARE\WJST\WLAN
and read it back?
Upvotes: 6
Views: 8503
Reputation: 1307
Sometimes if you try to access the key directly with key.SetValue()
throws an exception because you don't have the rights.
You can use Registry.SetValue()
like in the documentation or like following example:
const string root = "HKEY_LOCAL_MACHINE"; // Root of your path
const string path = @"\SOFTWARE\WJST\WLAN"; // Path of your key
const string default = "CDInsert"; // Name of the key
const string value = "1";
Registry.SetValue(root + "\\" + path, default, value);
Pay attention if your registry is volatile than you should flush and persist:
const UInt32 HKEY_LOCAL_MACHINE = 0x80000002;
IntPtr hkeyLocalMachine = new IntPtr(unchecked((int)HKEY_LOCAL_MACHINE ));
RegFlushKey(hkeyLocalMachine);
For use RegFlushKey
you need to add an extern method:
[DllImport("coredll.dll")]
public static extern int RegFlushKey(IntPtr hKey);
Usefull links: pinvoke.net, regflushkey.
Upvotes: 0
Reputation:
http://msdn.microsoft.com/en-us/library/microsoft.win32.registry%28v=VS.90%29.aspx
Try this:
//using Microsoft.Win32;
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WJST\WLAN", true);
// set value of "CDInsert" to 1
reg.SetValue("CDInsert", 1, RegistryValueKind.DWord);
// get value of "CDInsert"; return 0 if value not found
int value = (int)reg.GetValue("CDInsert", 0);
Upvotes: 6