配置文件:App.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ConnenctionString" value="Data Source=RUBY;Database=Huiyuan;User id=sa;PWD=" />
<add key="TmpPath" value="C:\Temp" />
</appSettings>
</configuration>
写XML:
//App.config配置文件写的方法
con = "Data Source=" + sql.Text.ToString() + ";Database=" + db.Text.ToString() + ";User id=" + usr.Text.ToString() + ";PWD=" + pwd.Text.ToString() + "";
SaveConfig("ConnenctionString", con);
public void SaveConfig(string configKey, string configValue)
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath +//app.config"); />
XmlNodeList nodes = doc.GetElementsByTagName("add");
foreach (XmlNode node in nodes)
{
XmlElement xe = (XmlElement)node;
if (xe.GetAttribute("key") == configKey)
{
xe.SetAttribute("value", configValue);
}
}
doc.Save(Application.StartupPath + //app.config");
}
读XML:
public static string GetValue(string appKey)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Application.StartupPath + //app.config);
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
浙公网安备 33010602011771号