Windows Develop Bookmark and Share   
 index > Windows Forms General > Creating an if statement based on reg value?
 

Creating an if statement based on reg value?

How would I go about creating an if/else statement, based on if a registry value exists?

Here's the code I have already for checking for a registry key. I'm looking for it to check for the registry value, of this key.

{
Microsoft.Win32.RegistryKey subKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
if (subKey != null)
{
MessageBox.Show("Registry Key exists");
}
else
{
MessageBox.Show("Registry Key does not exist");
}
}
TheTrueFace  Sunday, September 27, 2009 8:58 AM
Just try to read the value:

using System;
using Microsoft.Win32;

class Program {
static void Main(string[] args) {
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Nobugz");
if (key.GetValue("test", null) == null)
Console.WriteLine("value does not exist");
Console.ReadLine();
}
}

Hans Passant.
  • Marked As Answer byTheTrueFace Sunday, September 27, 2009 10:37 PM
  •  
nobugz  Sunday, September 27, 2009 12:22 PM
The only way I can see that GetValue() doesn't work is when you actually created a "Registry sample" key instead of a value. Right-click the Run key and select New + String Value, not New + Key.

Hans Passant.
  • Marked As Answer byTheTrueFace Sunday, September 27, 2009 10:37 PM
  •  
nobugz  Sunday, September 27, 2009 8:11 PM
You misspelled "Registery". Don't delete the Run key, that corrupts your Windows installation. You got lucky, UAC protected you.

Hans Passant.
  • Marked As Answer byTheTrueFace Sunday, September 27, 2009 10:37 PM
  •  
nobugz  Sunday, September 27, 2009 8:33 PM
Here is the code you can get values of this key.

            RegistryKey subKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            if (subKey != null)
            {
                MessageBox.Show("Registry Key exists");
                MessageBox.Show(subKey.ValueCount.ToString());
                foreach (string s in subKey.GetValueNames())
                {
                    MessageBox.Show(s + ":" + subKey.GetValue(s));
                }
            }
            else
            {
                MessageBox.Show("Registry Key does not exist");
            }
Tamer Oz  Sunday, September 27, 2009 9:24 AM
if (subKeyvalue == "RegistrySample")
{
}


I wanted something along the lines of that, the code will execute, if the specified registry value exists.
TheTrueFace  Sunday, September 27, 2009 10:19 AM
Just try to read the value:

using System;
using Microsoft.Win32;

class Program {
static void Main(string[] args) {
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Nobugz");
if (key.GetValue("test", null) == null)
Console.WriteLine("value does not exist");
Console.ReadLine();
}
}

Hans Passant.
  • Marked As Answer byTheTrueFace Sunday, September 27, 2009 10:37 PM
  •  
nobugz  Sunday, September 27, 2009 12:22 PM
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
if (key.GetValue("Registry Sample", null) == null)
{
MessageBox.Show("Registry Key exists");
}
else
{
MessageBox.Show("Registry Key does not exist");
}

Why does this not work for me? weather the key is deleted or not, it still says the registry key exists.
TheTrueFace  Sunday, September 27, 2009 7:19 PM
You've got the test backwards. Make sure you see the difference between keys and values. I assume you didn't delete the Run key.

Hans Passant.
nobugz  Sunday, September 27, 2009 8:02 PM
I actually don't delete the runkey, just it's value.

Which is why I simply need to read the value.

I need to read my software value of Registry Sample.

I do get the difference, sorry if those message boxes are confusing.
TheTrueFace  Sunday, September 27, 2009 8:05 PM
The only way I can see that GetValue() doesn't work is when you actually created a "Registry sample" key instead of a value. Right-click the Run key and select New + String Value, not New + Key.

Hans Passant.
  • Marked As Answer byTheTrueFace Sunday, September 27, 2009 10:37 PM
  •  
nobugz  Sunday, September 27, 2009 8:11 PM
On second thought, if you could tell me how I could delete that sub key, I guess that would work just fine.

RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
reg.DeleteValue("Registery Sample");
reg.DeleteSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

That errors and says the sub key doesnt exist when I try it.
TheTrueFace  Sunday, September 27, 2009 8:14 PM
You misspelled "Registery". Don't delete the Run key, that corrupts your Windows installation. You got lucky, UAC protected you.

Hans Passant.
  • Marked As Answer byTheTrueFace Sunday, September 27, 2009 10:37 PM
  •  
nobugz  Sunday, September 27, 2009 8:33 PM
Hm. what do I delete to get rid of the registry then?
TheTrueFace  Sunday, September 27, 2009 8:34 PM
Is there any way for me to just read the value Registry Sample from that key?............
TheTrueFace  Sunday, September 27, 2009 8:36 PM
Not quite sure how you missed GetValue(). What on Earth are you doing? Pop a beer, kick the dog, kiss the girl and get some sleep. It will all work tomorrow morning.

Hans Passant.
nobugz  Sunday, September 27, 2009 9:35 PM
Here's the code i was suggested to use to create an if statement based on IF the registry value existed.

{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
if (key.GetValue("Registry Sample", null) == null)
{
MessageBox.Show("Registry Value exists");
}
else
{
MessageBox.Show("Registry Value does not exist");
}
}

Here's my code for writing the value

if (this.startwithwindows.CheckState == CheckState.Checked)
{
RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
reg.SetValue("Registery Sample", Application.ExecutablePath.ToString());
MessageBox.Show("Load with windows has been enabled.");
}
else
{
RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
reg.DeleteValue("Registery Sample");
}


I can go to sleep after I'm done with this... fotunately...
TheTrueFace  Sunday, September 27, 2009 9:57 PM
Figured it out, thanks for all your help!
TheTrueFace  Sunday, September 27, 2009 10:37 PM

You can use google to search for other answers

Custom Search

More Threads

• Passing a random resource image file to Picturebox
• WinInet Don't work???
• How to make button to create new folder
• Error While Sending Mail From ASP>NET
• Closing One Form Then Opening Another
• How can one Dial an ISP from a Windows Application?
• File Version Checking Through Internet Application in VB.net
• Achieving high refresh rate on C# winform application
• Child and Parent Form
• Event that gets raised when the active window has changed