You'll have to read it in like any other xml file. This is chopped out
of a larger function, but you get the idea:
XmlDocument configXmlDoc = new XmlDocument();
configXmlDoc.Load("web.config");
XmlNodeList oList =
configXmlDoc.SelectNodes("configuration/appSettings/add");
for(int i = 0; i < oList.Count; i++)
{
XmlAttribute oKey = oList
.Attributes["key"];
XmlAttribute oValue = oList.Attributes["value"];
if(oKey.Value == "ConnectionString")
{
// parse conn string
Regex connStringRegex = new
Regex("SERVER=(.*);DATABASE=(.*);UID=(.*);PWD=(.*);",
RegexOptions.IgnoreCase);
Match match = connStringRegex.Match(oValue.Value);
dbServerText.Text = match.Groups[1].Captures[0].ToString();
dbNameText.Text = match.Groups[2].Captures[0].ToString();
dbUserText.Text = match.Groups[3].Captures[0].ToString();
dbPasswordText.Text = match.Groups[4].Captures[0].ToString();
}
}
// write the config settings
XmlNodeList oList =
configXmlDoc.SelectNodes("configuration/appSettings/add");
for(int i = 0; i < oList.Count; i++)
{
XmlAttribute oKey = oList.Attributes["key"];
XmlAttribute oValue = oList.Attributes["value"];
if(oKey.Value == "ConnectionString")
{
oValue.Value =
string.Format("SERVER={0};DATABASE={1};UID={2};PWD={3};",
dbServerText.Text, dbNameText.Text, dbUserText.Text,
dbPasswordText.Text);
}
}
configXmlDoc.Save("Web.config");
-Jason
Thank you, everybody for your replies.
The idea is to configure database connections and write the configuration
information into web.config, the utility will be used by end users. They
will install the ASP.NET application and the utility will be also installed
into the same directory. They will run the utility and it will create
encrypted entries in web.config. I want to make it as easy for the end user
as possible, I wouldn't want to go into registry.
I used to do the same thing with ini files for win 32 applications, it was
easy to write encrypted database connection settings into ini files from a
program that does encryption.
The information is static because the db connections will be very seldom
reconfigured.