Hi all , i just wanted to know ,why the **** this thing doenst work , i've tested the code 10 mins ago it worked ,and now ,it doesnt .

        static private bool ExistanceCheck(string Start, string SubKeyPath, string KeyName, string DefaultVal)//true , changed, false (unexistante , other value)
        {
            RegistryKey rk = Registry.LocalMachine;//fcking c#
            if (Start == "HKLM")
            {
                rk = Registry.LocalMachine;
            }
            else if (Start == "HKCU")
            {
                rk = Registry.CurrentUser;
            }


            rk = rk.OpenSubKey(SubKeyPath);
            object valor = rk.GetValue(KeyName);
            try
            {
            if (valor.ToString() == "")
            {
                return false;
            }
            }
            catch (Exception)
            {
                return false;

            }
return true;

        }

I call it with : bool xyz = ExistanceCheck("HKCU", "SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "DisableRegistryTools", "0");

Btw ,to it to work we need using Microsoft.Win32; .

Recommended Answers

All 4 Replies

Hello, jen140.
If I understand you correctly, here:

if (valor.ToString() == "")

you're check if the operation rk.GetValue(KeyName); returned nothing to the 'valor' variable. Try to change it on this:

if (valor == null)

Ty for the quick answer .
It worked with "" .
And i only now saw ,that i've didnt show the erorr , here it is :
Object reference not set to an instance of an object.
At object valor = rk.GetValue(KeyName); .

Well, then I have no idea .. O.o
I get same exception ("Object reference not set to an instance of an object.") when after this:

object valor = rk.GetValue(KeyName);

the value remains 'null'. And after trying to cast it to String here:

if (valor.ToString() == "")

I get this exception.

Very nice :\ like 10 mins ago it was working just fine .
I just changed the input values , and boom , it doesnt work:
Now just tried :

        RegistryKey rk = Registry.LocalMachine;
        rk = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
        object xvalor = rk.GetValue("Skytel");
        Console.Write(xvalor);

And everithink worked .
Even with:

    static private bool ExistanceCheck2(string Start, string SubKeyPath, string KeyName, string DefaultVal)//true , hacked , false (unexistante , other value)
    {
        RegistryKey rk = Registry.LocalMachine;
        rk = rk.OpenSubKey(SubKeyPath);
        object valor = rk.GetValue(KeyName);
        return true;
    }

And calling

bool xyz = ExistanceCheck2("HKCU", "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", "DisableRegistryTools", "0");

Works fine.
So now i found an error ,its somewhere in:

        if (Start == "HKLM")
        {
            rk = Registry.LocalMachine;
        }
        else if (Start == "HKCU")
        {
            rk = Registry.CurrentUser;
        }

Im getting to love c#, now if i have "HKLM" , and not the "HKCU" ,it works...
So the error is in : rk = Registry.CurrentUser; , but obviosly ,if i try it directly it works...

        RegistryKey rk = Registry.CurrentUser;
        rk = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
        object xvalor = rk.GetValue("Skytel");
        Console.Write(xvalor);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.