How do I parse name/value pairs from a config string?

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?
 
Bill,
I normally use String.Split to separate each pair, then use String.Split
again to separate the pairs.

Optionally putting the name/value pairs into a HashTable or
NameValueCollection.

Instead of the second String.Split I've used String.IndexOf &
String.SubString to split the pairs, as there should be only a single split
needed...

Hope this helps
Jay
 
Yep. You may use String.Split() to get individual ';' separated items and
another String.Split() (or String.StartsWith() ) on the first set to get the
password.

HTH.

If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?
 
If you wanted to get really excited you could use Regular Expressions:

In VB.NET (I doubt the C# is a million miles away) :

'don't forget to import the namspace
Imports System.Text.RegularExpressions

'later...
'we're assuming a SQL Server connection string here and SQL Server
'Authentication
Dim regExp as new Regex("server=\w*;")
Dim m Match

m = regExp.Match(strDBConnectionString, "pwd=\w*;")

If m.Success Then
'you've found it...
End If

Notice the "\w*" bit is searching for a "word" in the phrase searched.

This would work with the other bits of the connection string too by tweaking
the expression BUT I have found the "word" gets confused if your server is
being identified by an IP address instead of it's name...

Probably all those "."'s :)

(e-mail address removed)
 
Thanks... Reqular Expressions strikes me as the way to go.

alaspin said:
If you wanted to get really excited you could use Regular Expressions:

In VB.NET (I doubt the C# is a million miles away) :

'don't forget to import the namspace
Imports System.Text.RegularExpressions

'later...
'we're assuming a SQL Server connection string here and SQL Server
'Authentication
Dim regExp as new Regex("server=\w*;")
Dim m Match

m = regExp.Match(strDBConnectionString, "pwd=\w*;")

If m.Success Then
'you've found it...
End If

Notice the "\w*" bit is searching for a "word" in the phrase searched.

This would work with the other bits of the connection string too by tweaking
the expression BUT I have found the "word" gets confused if your server is
being identified by an IP address instead of it's name...

Probably all those "."'s :)

(e-mail address removed)

Bill said:
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?
 
Back
Top