tags:

views:

76

answers:

4

I am getting an error means intellisense not producing 'regkey' variable, when i am assigning value in to the property.Can we assign value in to a property like this way

like this way

 public string Wrmversion
   {
       get { return m_wrmversion; }
       set { m_wrmversion = value; }
   }

   private string m_wrmversion = (string)regkey.GetValue(SpoRegistry.regValue_CurrentVersion);

where i declared regkey as it is

string keySpoPath = SpoRegistry.WrmSpoAgentRoot;//here i am giving registry path
           RegistryKey regkey = Registry.LocalMachine.OpenSubKey(keySpoPath);
+1  A: 

Why not do this in class constructor?

P.S.: regkey variable should be declared in constructor body.

Unholy
+2  A: 

this is a bad practice to use properties for this scope.

If you have a probability to have exceptions or errors, use functions.

In this way you can surround it with try/catch blocks.

So, I'd reccomand

string GetVersion() 
{
    return regkey.GetValue(SpoRegistry.regValue_CurrentVersion).ToString();
}
serhio
+2  A: 

Your code looks fine. You should post your exception it would help us give a more informed answer. It could be a number of things, see the RegistryKey.GetValue method on MSDN for more information regarding the type of exceptions that are thrown. In general though you should verify:

1) You have authority to access the registry.
3) You have authority to access the specific key.
2) The key you are trying to read exits and is the correct format i.e. a string value

There is an Overload for the GetValue method which allows you to specify a default value if the key is not found.

James