Checking and modifying registry keys using C# in Vista


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:


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 =

                // 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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
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");
    MessageBox.Show("Registry Key does not exist");

you can use the subKey.SetValue function to set the value of the key
John500Author Commented:

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

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

Any ideas?

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

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

Anurag ThakurTechnical ManagerCommented:
you can use this sample and iterate through all the sub keys

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John500Author Commented:
Ok, last question.  In that last MSDN example you provided, this is how the sub value is set for new subvalues:

            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?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.