Reading Registry Values

  • Thread starter Thread starter Andrew Raastad
  • Start date Start date
A

Andrew Raastad

I have been attempting to update a Windows Service that was written a couple
years ago (original programmer no longer here). I have been able to get the
project to work perfectly on the development machine, but when I attempted
to run it on the production box... nothing. So, I built a quick little
WinForm app that mimics the code of the Windows Service (debugging services
is a such a pain), but in this case it spits out to the screen process
messages at various points so I can see where it is going fine and where it
"breaks". And it is "breaking", apparently, on reading from the registry.

Code I am using to read the registry value:

Private Function GetRegKey(ByVal Item As String, Optional ByVal NoValRet
As String = "") As String
Dim Result As String = ""
Try
Dim regFolder As String = "Software\MARS\Integrations"
Dim regKey As RegistryKey
WriteEvent("Accessing SubKey value """ & Item & """ within
[HKEY_LOCAL_MACHINE\" & regFolder.ToUpper & "]")
regKey = Registry.LocalMachine.OpenSubKey(regFolder, True)
Result = regKey.GetValue(Item, NoValRet).ToString

Catch ex As Exception
Result = NoValRet

End Try
Return Result
End Function

The above "WriteEvent()" is just for my test app so I can see what it
returned. And when I run the app, it returns "". I fire up RegEdit, go to
the location specified, and there IS a value for each item in there, and I
have also verified that the spelling of those items EXACTLY matches the
values given as the above "Item" method argument. There should be no reason
I am getting a "" returned.

I run this code on my Dev box, I get values. I run it on the production
box, I get "", and both have the EXACT same registry setup and values. The
only difference is my dev box is Win Vista, and the production box is Win
Server 2003. But this shouldn't make a difference, should it? Is there
something wrong with the above code?

-- Andrew
 
You were right, the way the Catch was written I was missing the error being
thrown (though I don't quite follow what you are trying to say with the
Opening/Closing balance thing).

Anyhow, I am now getting this error -- on the Production box *only*:

System.Exception: GetRegKey() Failed:
System.Security.SecurityException: Request for the permission of type
'System.Security.Permissions.RegistryPermission, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at Microsoft.Win32.RegistryKey.CheckSubKeyReadPermission(String
subkeyName)
at Microsoft.Win32.RegistryKey.CheckOpenSubKeyPermission(String
subkeyName, Boolean subKeyWritable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at NP2Exchange.Form1.GetRegKey(String Item, String NoValRet)


What is it I need to do in order to READ from this registry location?

-- Andrew


Patrice said:
Most often this is a security issue. Also having an Opening call without a
balancing Closing call is always suspicious...

I would recommend finally to change a bit the exception handler. IMO the
goal is no to keep an application running whatever goes wrong but to get
detailed information so that you can fix the error. Write down the
exception details rather than to ignore it. It should give a precious clue
about why the read doesn't work


--
Patrice

Andrew Raastad said:
I have been attempting to update a Windows Service that was written a
couple years ago (original programmer no longer here). I have been able
to get the project to work perfectly on the development machine, but when
I attempted to run it on the production box... nothing. So, I built a
quick little WinForm app that mimics the code of the Windows Service
(debugging services is a such a pain), but in this case it spits out to
the screen process messages at various points so I can see where it is
going fine and where it "breaks". And it is "breaking", apparently, on
reading from the registry.

Code I am using to read the registry value:

Private Function GetRegKey(ByVal Item As String, Optional ByVal
NoValRet As String = "") As String
Dim Result As String = ""
Try
Dim regFolder As String = "Software\MARS\Integrations"
Dim regKey As RegistryKey
WriteEvent("Accessing SubKey value """ & Item & """ within
[HKEY_LOCAL_MACHINE\" & regFolder.ToUpper & "]")
regKey = Registry.LocalMachine.OpenSubKey(regFolder, True)
Result = regKey.GetValue(Item, NoValRet).ToString

Catch ex As Exception
Result = NoValRet

End Try
Return Result
End Function

The above "WriteEvent()" is just for my test app so I can see what it
returned. And when I run the app, it returns "". I fire up RegEdit, go
to the location specified, and there IS a value for each item in there,
and I have also verified that the spelling of those items EXACTLY matches
the values given as the above "Item" method argument. There should be no
reason I am getting a "" returned.

I run this code on my Dev box, I get values. I run it on the production
box, I get "", and both have the EXACT same registry setup and values.
The only difference is my dev box is Win Vista, and the production box is
Win Server 2003. But this shouldn't make a difference, should it? Is
there something wrong with the above code?

-- Andrew
 
Back
Top