Click to See Complete Forum and Search --> : checking for a value in the registry


summey
April 26th, 2009, 02:16 PM
I have my code it works i get back the registry value but what i want to do is

check to see if its empty if its empty ok to install program

if not would you like to remove the registry value?

here is my code it dosnt work if i put somthing in the reg value and run my program it dosnt check
if i remove the data from that value same results.

how can i set this up below is my code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;


namespace regMod
{
class Program
{
static void Main(string[] args)
{


RegistryKey MyReg = Registry.LocalMachine.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\setup.exe", true);

try
{

string IsVal = (string)MyReg.GetValue("Default", "");

Console.WriteLine(MyReg.GetValue(IsVal).ToString());

if (IsVal == null)
{

Console.WriteLine("ok to install");
}
else
{
Console.WriteLine("Remove key value data?");

}
}





thank you for your time
-summey

ozzy66
April 27th, 2009, 03:35 AM
I have my code it works i get back the registry value but what i want to do is

check to see if its empty if its empty ok to install program

if not would you like to remove the registry value?

here is my code it dosnt work if i put somthing in the reg value and run my program it dosnt check
if i remove the data from that value same results.

how can i set this up below is my code

sorry, I don't get it, what do you mean by ' it dosnt check' if you put smth in the reg value? Which part of the if-clause is run through?

yuenqi
April 27th, 2009, 04:44 AM
You open without close, :D becareful of bacteria entry :D

summey
April 27th, 2009, 07:58 AM
please check this image out www.summeylabs.com/images/reg.jpg

to show you what im talking about visually.

i just want it to check to see if the the value has data if the data is empty then print ok if the data isnt emptoy print not empty

my program dosnt work if i have data in the there or not still prints the same answer.

Thank you for your time.

-Summey

ozzy66
April 28th, 2009, 12:15 AM
http://msdn.microsoft.com/en-us/library/xzxd58z1.aspx
A registry key can have one value that is not associated with any name. When this unnamed value is displayed in the registry editor, the string "(Default)" appears instead of a name. To retrieve this unnamed value, specify either null/Nothing/nullptra null reference (Nothing in Visual Basic) or the empty string ("") for name.

summey
April 28th, 2009, 08:02 AM
Thank you for all the advice. I got it working and how i did it was.

its not null when you set it to "" because it puts "" in binary more zeros left be hind thus leaving invisible data.

so if i right click on (default) in the reg and hit delete it sets the value field to (value not set) thus making it null and my program works.

my questions is how do i properly delete it to so its back to value not set ?

everything works





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;


namespace regMod
{
class Program
{
static void Main(string[] args)
{



RegistryKey MyReg = Registry.LocalMachine.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\setup.exe", true);

string IsVal = (string)MyReg.GetValue("Default");

object ValData = MyReg.GetValue(IsVal);


if (ValData != null)
{

string stringData = ValData.ToString();
Console.WriteLine(MyReg.GetValue(IsVal).ToString());

Console.WriteLine("removing value now");

MyReg.SetValue("","");
MyReg.Close();


}
else
{

Console.WriteLine("Empty");
MyReg.Close();

}
}
}
}


thank you