mmattax
mmattax

Reputation: 27660

How can I programmatically change a value in the Window's Registry?

I need to programmatically change the "Level" String found in \HKEY_CURRENT_USER\Software\Intuit\QBWebConnector to "Verbose"

What is the best way to do this? C#, bat file? I have never tinkered with the registry before...

Thanks.

Upvotes: 11

Views: 9578

Answers (5)

Hadi
Hadi

Reputation: 110

If you want to edit programmatically registry file, the best way (in my opinion) is the C#! you can use these codes to add, edit or delete registry keys:

Add or Edit:

Registry.SetValue("HKEY_LOCAL_MACHINE\\" + keyPath , "KeyName", "KeyValue", RegistryValueKind.String);

Delete:

using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var myKey = hklm.OpenSubKey(keyPath, true))
{
     if (myKey == null)
         MessageBox.Show("Register alredy deleted");
     else
     {
        myKey.DeleteValue("KeyName");
        myKey.Close();
        MessageBox.Show("Register deleted!");
      }
 }

Important point:

your app must be run as an administrator! for this, you can add a manifest to your app.

This is a simple sample to edit registery file:

https://github.com/hgh6484/AddKeyToRegistry

Upvotes: 0

User5910
User5910

Reputation: 485

Here are some more ways in order of easyness not mentioned above:

  1. Reg.exe add - Type reg /? to see options and the Reg reference for details.
  2. Regini.exe [scriptfile] - More powerful than .reg files, you can delete subkeys and data items and set permissions. Type regini /? to see instructions or the MSDN article Distributing Registry Changes for details.
  3. ATL::CRegKey class, SetStringValue() member, see MSDN.
  4. .NET Registry Class SetValue() (C#, C++, F#, VB) - See MSDN reference.
  5. Win32 APIs such as RegSetKeyValue(). See the Registry Functions reference.

Upvotes: 6

Adrian Grigore
Adrian Grigore

Reputation: 33318

If the registry entry you are going to change is already in the registry, the simplest way to create a *.reg file that changes the registry entry as you need it is as follows:

  1. Open Regedit
  2. Locate the registry folder right above the registry key you are going to change in the treeview on the left.
  3. Right-click the folder and select "Export".
  4. Open the file you just exported with notepad and delete anything apart from the first line ("Windows Registry Editor Version 5.00" or similar), the folder name ( [HKEY_CURRENT_USER\Software\Intuit\QBWebConnector] in your case) and the name value pair for the key you would like to change
  5. Edit the value you would like to change appropriately and save the file. In your case you should end up with the following *.reg file:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Intuit\QBWebConnector]

"Level"="Verbose"

Double-clicking the file and confirming the security warning(s) will perform the changes on your registry.

Or, in a batchfile, you can silently import the registry file via "REGEDIT /S pathname"

Be careful with the registry since you might otherwise wreck your windows installation.

Upvotes: 7

Joey
Joey

Reputation: 354366

From a batch file you may use reg.exe.

Upvotes: 1

emk
emk

Reputation: 60922

Actually, the easiest way to change a bunch of registry keys is to use a *.reg file and simply load it into the registry. But be careful: You generally can't send these files to people via e-mail, because they get filtered by many mail servers.

We occasionally use this technique to pass around application configurations and test them on other machines.

I only mention this non-programmatic solution because you suggested that either a C# application or a batch file would be OK, which suggests that you're looking for something lightweight and you aren't too worried about the details.

Upvotes: 2

Related Questions