?
Solved

Checking and modifying registry keys using C# in Vista

Posted on 2008-11-03
6
Medium Priority
?
716 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:John500
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22868996
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
0
 

Author Comment

by:John500
ID: 22869222
Thanks.

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

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

Any ideas?

Thanks
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22869310
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:John500
ID: 22869337
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
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 2000 total points
ID: 22869596
you can use this sample and iterate through all the sub keys
http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey(VS.80).aspx
0
 

Author Comment

by:John500
ID: 22870193
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Unable to change the program that handles the scan event from a network attached Canon/Brother printer/scanner. This means you'll always have to choose which program handles this action, e.g. ControlCenter4 (in the case of a Brother).
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question