467,905 Members | 1,840 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,905 developers. It's quick & easy.

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

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?
Jul 21 '05 #1
  • viewed: 12388
Share:
4 Replies
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

"Bill" <nf*@nospam.com> wrote in message
news:Ot*************@TK2MSFTNGP12.phx.gbl...
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?

Jul 21 '05 #2
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.

"Bill" <nf*@nospam.com> wrote in message
news:Ot*************@TK2MSFTNGP12.phx.gbl...
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?

Jul 21 '05 #3
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 :)

al*****@yahoo.com

"Bill" wrote:
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?

Jul 21 '05 #4
Thanks... Reqular Expressions strikes me as the way to go.

"alaspin" <al*****@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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 :)

al*****@yahoo.com

"Bill" wrote:
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?

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by brettr | last post: by
5 posts views Thread by Robert Oschler | last post: by
21 posts views Thread by William Stacey [MVP] | last post: by
5 posts views Thread by Navid Azimi | last post: by
7 posts views Thread by Cirene | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.