# [SOLVED] C# .Net Framework error on XP



## Emphyrio (May 16, 2009)

_Testing env : VM Windows XP Pro SP3, .Net 4.0 installed
C# Code target : All CPU x86 app Manifest (admin rights elevated)_

I want to write a code that displays the value of ""LastSuccessTime"" in 
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Detect"

If not present it gives back "Update detected: n/a"

The code (peace of):


```
private void Form_PCinfo_Load(object sender, EventArgs e)
{ 
RegistryKey rk = Registry.LocalMachine;
string sKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\Results\\";
subKey = rk.OpenSubKey(sKey + "Detect");
if (subKey != null)
  {
  lblUPde.Text = "Update detected: " + subKey.GetValue("LastSuccessTime").ToString();
  }
  else
  {
    lblUPde.Text = "Update detected: n/a";
  } 
}
```
Gives me the folowing error:










No error on Vista and W7 x64


Registery Outputs (from the XP machine):



> Windows Registry Editor Version 5.00
> 
> [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results]
> 
> ...



Seems like " if (subKey != null) " is not working because when subKeys isn't emphty or does exist, I don't get the error.

Any ideas to how to overcome this?


----------



## CEM (Jan 5, 2011)

*Re: C# .Net Framework error on XP*

Can you check this link might be help your issue.

" if (subKey != null) " is not working because when subKeys isn't emphty or does exist - MSDN Search


----------



## Emphyrio (May 16, 2009)

*Re: C# .Net Framework error on XP*

Hi betranu,

Thanks for the reply. 

I don't see (or can't find) any relevant topic nor solution in the proposed link for this specific problem.

I also tried to encapsulated the code in a "Try...Catch (throw)", but that doesn't work either.


----------



## Emphyrio (May 16, 2009)

*Re: C# .Net Framework error on XP*

After a longtime fo code bugging I tried this code:



```
private void Form_PCinfo_Load(object sender, EventArgs e)
{ 
string sKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\Results\\";
RegistryKey rk = Registry.LocalMachine;
subKey = rk.OpenSubKey(sKey + "Detect");

try
{
subKey = rk.OpenSubKey(sKey + "Detect", true);
if (subKey == null)
{
throw new Exception();
}
lblUPde.Text = "Update detected: " + subKey.GetValue("LastSuccessTime").ToString();
}
catch (Exception ex)
{
lblUPde.Text = "Update detected: n/a";
}
}
```
Changing the checking method on _subKey_ did the trick. :smile:


----------

