tags:

views:

146

answers:

1

Hello all

I am trying to get values and set values to the Registry .

When I am trying to access a path that not located at the registry I am getting exaption .

But when i am setting that path with Registry.SetValue(keyName, "", 0);, all works fine and I can get non existing values from it .

Any idea why i can't use my public int GetComponent(string RegKey) function on nun existing paths ?

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


namespace LP_Misc
{
    public class LP_Registery
    {
        private const string userRoot = "HKEY_CURRENT_USER";
        private const string subkey = @"Software\PCBMatrix\LPWizard";
        private  string keyName ;

        public LP_Registery(string folderName)
        {
            keyName = userRoot + "\\" +  subkey + "\\" + folderName;
            //Registry.SetValue(keyName, "", 0);

        }
        public int GetComponent(string RegKey)
        {
            return (int)Registry.GetValue(keyName, RegKey, 0);

        }
        public void SetComponent(string RegKey, int RegVal)
        {
            Registry.SetValue(keyName, RegKey, RegVal, RegistryValueKind.DWord);
        }



    }
}

And if it possible what should i do that it will be possible .

Thanks .

+1  A: 

surround the GetValue with try - catch block and enjoy the coding.

Exceptions and Conditions of GetValue:

SecurityException: The user does not have the permissions required to read from the registry key.

IOException: The RegistryKey that contains the specified value has been marked for deletion.

ArgumentException: keyName does not begin with a valid registry root.


SetValue method:

Sets the specified name/value pair on the specified registry key. If the specified key does not exist, it is created.

serhio