Hi.
I am programming a VB.Net Windows Service Application. This service uses a file watcher to watch a directory for XML files - then it goes to process the files etc. This is not the problem.
The problem is that to set the file watcher directory path I need to read this from the Registry. I have coded a read registry function that works fine in my other VB.Net applications however when I add it to my WebService it does not return the value of the registry setting it returns an error "Object reference not set to an instance of an object.". I have included the code below and as I say this works in my other n-tier VB.Net Applications - is there any difference in using reading of the registry in a Windows Service Application?
Thanks in advance.
Code:
Private m_strRequestPath As System.String
Protected Overrides Sub OnStart(ByVal args() As String)
'Service OnStart event.
'
Try
'Set Request File Directories
m_strRequestPath = GetDirectory(SpecifyDirectory.eRequestPath)
Catch ex As System.Exception
Throw
End Try
End Sub
Public Function GetDirectory() As System.String
Try
GetDirectory = GetRegistryValue(Microsoft.Win32.RegistryHive.CurrentUser.CurrentUser, _
"Software\VB and VBA Program Settings\WorkCentre\Paths", _
"PolarisAssembly".Trim
GetDirectory &= "XML\REQUEST\"
Return GetDirectory
Catch ex As System.Exception
Throw
Finally
End Try
End Function
Private Function GetRegistryValue(ByVal regHive As Microsoft.Win32.RegistryHive, _
ByVal strKey As System.String, _
ByVal strValueName As System.String _
) As System.String
Dim objParent As Microsoft.Win32.RegistryKey
Dim objSubkey As Microsoft.Win32.RegistryKey
Dim strValue As System.String = ""
Try
'Select case on the registry hive to open the correct registry class
Select Case regHive
Case Microsoft.Win32.RegistryHive.ClassesRoot
objParent = Microsoft.Win32.Registry.ClassesRoot
Case Microsoft.Win32.RegistryHive.CurrentConfig
objParent = Microsoft.Win32.Registry.CurrentConfig
Case Microsoft.Win32.RegistryHive.CurrentUser
objParent = Microsoft.Win32.Registry.CurrentUser
Case Microsoft.Win32.RegistryHive.DynData
objParent = Microsoft.Win32.Registry.DynData
Case Microsoft.Win32.RegistryHive.LocalMachine
objParent = Microsoft.Win32.Registry.LocalMachine
Case Microsoft.Win32.RegistryHive.PerformanceData
objParent = Microsoft.Win32.Registry.PerformanceData
Case Microsoft.Win32.RegistryHive.Users
objParent = Microsoft.Win32.Registry.Users
End Select
'Once the correct class is open try to initialise the
'appropriate key specified by the user
objSubkey = objParent.OpenSubKey(strKey)
'If the key we are looking for is not found then
'the object will not be initialised otherwise it will
If Not objSubkey Is Nothing Then
strValue = (objSubkey.GetValue(strValueName).ToString)
If strValue Is Nothing Then
strValue = ""
End If
End If
Catch ex As System.Exception
Throw
Finally
'If strValue is empty the registry setting is blank or does not exist
If strValue.Trim = "" Then
GetRegistryValue = "ERROR"
Else
GetRegistryValue = strValue.Trim
End If
objSubkey = Nothing
objParent = Nothing
End Try
End Function
I am programming a VB.Net Windows Service Application. This service uses a file watcher to watch a directory for XML files - then it goes to process the files etc. This is not the problem.
The problem is that to set the file watcher directory path I need to read this from the Registry. I have coded a read registry function that works fine in my other VB.Net applications however when I add it to my WebService it does not return the value of the registry setting it returns an error "Object reference not set to an instance of an object.". I have included the code below and as I say this works in my other n-tier VB.Net Applications - is there any difference in using reading of the registry in a Windows Service Application?
Thanks in advance.
Code:
Private m_strRequestPath As System.String
Protected Overrides Sub OnStart(ByVal args() As String)
'Service OnStart event.
'
Try
'Set Request File Directories
m_strRequestPath = GetDirectory(SpecifyDirectory.eRequestPath)
Catch ex As System.Exception
Throw
End Try
End Sub
Public Function GetDirectory() As System.String
Try
GetDirectory = GetRegistryValue(Microsoft.Win32.RegistryHive.CurrentUser.CurrentUser, _
"Software\VB and VBA Program Settings\WorkCentre\Paths", _
"PolarisAssembly".Trim
GetDirectory &= "XML\REQUEST\"
Return GetDirectory
Catch ex As System.Exception
Throw
Finally
End Try
End Function
Private Function GetRegistryValue(ByVal regHive As Microsoft.Win32.RegistryHive, _
ByVal strKey As System.String, _
ByVal strValueName As System.String _
) As System.String
Dim objParent As Microsoft.Win32.RegistryKey
Dim objSubkey As Microsoft.Win32.RegistryKey
Dim strValue As System.String = ""
Try
'Select case on the registry hive to open the correct registry class
Select Case regHive
Case Microsoft.Win32.RegistryHive.ClassesRoot
objParent = Microsoft.Win32.Registry.ClassesRoot
Case Microsoft.Win32.RegistryHive.CurrentConfig
objParent = Microsoft.Win32.Registry.CurrentConfig
Case Microsoft.Win32.RegistryHive.CurrentUser
objParent = Microsoft.Win32.Registry.CurrentUser
Case Microsoft.Win32.RegistryHive.DynData
objParent = Microsoft.Win32.Registry.DynData
Case Microsoft.Win32.RegistryHive.LocalMachine
objParent = Microsoft.Win32.Registry.LocalMachine
Case Microsoft.Win32.RegistryHive.PerformanceData
objParent = Microsoft.Win32.Registry.PerformanceData
Case Microsoft.Win32.RegistryHive.Users
objParent = Microsoft.Win32.Registry.Users
End Select
'Once the correct class is open try to initialise the
'appropriate key specified by the user
objSubkey = objParent.OpenSubKey(strKey)
'If the key we are looking for is not found then
'the object will not be initialised otherwise it will
If Not objSubkey Is Nothing Then
strValue = (objSubkey.GetValue(strValueName).ToString)
If strValue Is Nothing Then
strValue = ""
End If
End If
Catch ex As System.Exception
Throw
Finally
'If strValue is empty the registry setting is blank or does not exist
If strValue.Trim = "" Then
GetRegistryValue = "ERROR"
Else
GetRegistryValue = strValue.Trim
End If
objSubkey = Nothing
objParent = Nothing
End Try
End Function