Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

name value pairs checking if they exist 1

Status
Not open for further replies.

ksbigfoot

Programmer
Apr 15, 2002
856
CA
I am reading from the app.config file successfully using the following code.
Code:
'Read in String from app.config file
Dim VarSettings As New System.Configuration.AppSettingsReader()
Dim oString As Object = VarSettings.GetValue("LogPath", GetType(String))
Dim strPath As String = oString.ToString()

How do I check if the name value pair "LogPath" doesn't exist?
 
The following link shows how to get all of the settings and loop through them. You could use something similar to check if the key you want exists.




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I have "Imports System.Configuration", but the ConfigurationManager is not coming up in the list.
So I can't get this line:
ConfigurationManager.AppSettings

Do you know why?
 
Since you are working with an app config file, let me make a jump and guess that if for some reason the LogPath is not found you need to assign some sort of default value. if that is correct, wouldn't something like this work??

Code:
'Read in String from app.config file
Dim VarSettings As New System.Configuration.AppSettingsReader()
Dim oString As Object
Try
    oString = VarSettings.GetValue("LogPath", GetType(String))
Catch ex As Exception
    ' I would also find the exact error that occurs when
    ' pair is not present and use that instead of the
    ' generic Exception
   oString = [i]your default value[/i]
End Try
Dim strPath As String = oString.ToString()

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 

It seems that ConfigurationManager.AppSettings is available in the .NET Framework 2.0 and above. Are you using an earlier version, i.e. .NET 1.1?

Also, try this and see what happens:

Dim oString As Object = VarSettings.GetValue("LogPath", GetType(String))

Dim strPath As String

If oString Is Nothing Then
'action when oString is nothing
Else
strPath = oString.ToString()
End If


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
That works perfectly.
Thank you and star to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top