shamim
shamim

Reputation: 6768

Way to write on registry location

work on C# window application.I want to write on registry.i know how to write on registry.I use bellow syntax to write on registry.

        Microsoft.Win32.RegistryKey key;
        key = Microsoft.Win32.Registry.CurrentUser.cre.CreateSubKey("asb");
        key.SetValue("asb", "Isabella");
        key.Close();

But problem is i fail to write on specified location .i want to write on bellow location

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

On this location want to add string value="abc" and ValueData="efd" If have any query plz ask.thanks in advance.

Upvotes: 3

Views: 4371

Answers (3)

Pankaj
Pankaj

Reputation: 10115

RegistryKey reg = Registry.LocalMachine.
                  OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

// set value of "abc" to "efd" 
reg.SetValue("abc", "efd", RegistryValueKind.DWord);

// get value of "abc"; return 0 if value not found 
string value = (string)reg.GetValue("abc", "0");

Upvotes: 2

Alex K.
Alex K.

Reputation: 175936

For HKCU:

string keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyName, true);
rk.SetValue("abc", "efd");
rk.Close();

For HKLM you need to do it with administrative privileges. That requires adding a manifest to your program to invoke the UAC prompt on Vista or Win7.

Upvotes: 8

Cody Gray
Cody Gray

Reputation: 244971

Writing to HKEY_LOCAL_MACHINE requires administrative privileges. And if you're running on Windows Vista or 7, it also requires process elevation, lest you run afoul of UAC (User Account Control).

The best thing is only to write to this registry key during installation (where you will have full administrative privileges). You should only read from it once your application is installed.

Save all regular settings under HKEY_CURRENT_USER. Use the Registry.CurrentUser field to do that. Or, better yet, abandon the registry altogether and save your application's settings in a config file. Visual Studio has built-in support for this, it's very simple to do from C#. The registry is no longer the recommended way of saving application state.

Upvotes: 2

Related Questions