Link to home
Start Free TrialLog in
Avatar of John500
John500Flag for United States of America

asked on

Checking and modifying registry keys using C# in Vista

Greetings,

I'm trying to check a particular set of keys to see if they exist.  If they exist I need to update them, with a 1 and a 0.

The path is:

"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\"

The keys:

ProfileName
Description

The values to check:

"CompanyX"       (For ProfileName)
"CompanyY"       (For Description)

I could use help on the syntax.  More specifically, how would this code be modified:

using Microsoft.Win32;

class Program
    {
        static void Main(string[] args)
        {
            RegistryKey OurKey = Registry.so

            try
           
            {  
                // check if 'ProfileName' exists - not null
              if ((int)Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\").GetSubKeyNames ??
                        Set its value to 1
               // check if 'Description' exists - not null
              if ((int)Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\NT\CurrentVersion\NetworkList\Profiles\").GetSubKeyNames ??      
                       Set its value to 1                                                          
            }
            catch (Exception excpt)
            {
                ProjObjects.Tools.NotifException.NotifExcep(excpt, null, "Registry Check Failure ");
            }

        }
    }

As you can see above, I'm not sure of the syntax for the two keys and how to check for null.
The exception would be if something unexpected were to happen and not if the keys don't exist.

Thanks

Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

to check if the key exists
Microsoft.Win32.RegistryKey subKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles");

If (subKey != null)
    MessageBox.Show("Registry Key exists");
else
    MessageBox.Show("Registry Key does not exist");

you can use the subKey.SetValue function to set the value of the key
Avatar of John500

ASKER

Thanks.

In terms of setting the value, I get the error:

Cannot assign to 'SetValue' because it is a 'method group'

Any ideas?

Thanks
Avatar of John500

ASKER

Also, I need to check the 'subkeys' within 'Profiles'.  Thus, how would I check to see if the subkey 'ProfileName' has the value 'CompanyX' ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John500

ASKER

Ok, last question.  In that last MSDN example you provided, this is how the sub value is set for new subvalues:

using(RegistryKey
            testName = test9999.CreateSubKey("TestName"),
            testSettings = test9999.CreateSubKey("TestSettings"))
        {
            // Create data for the TestSettings subkey.
            testSettings.SetValue("Language", "French");
            testSettings.SetValue("Level", "Intermediate");
            testSettings.SetValue("ID", 123);
            testSettings.SetValue("Password", "Secret");
        }
 
... but what if 'ID' already existed - how would you update the value?

Thanks