System.Xml.XmlDocument domSysinfo=new System.Xml.XmlDocument();
System.Xml.XmlNode root;
System.Xml.XmlNode node;
System.Xml.XmlNodeList nodeList;
domSysinfo.Load(AppDomain.CurrentDomain.BaseDirectory + "//app.config");
root=domSysinfo.SelectSingleNode("//configuration");
node=root.SelectSingleNode("//appSettings");
nodeList=node.SelectNodes("add");
for(int i=0;i<nodeList.Count;i++)
{
if(nodeList.Attributes["key"].InnerText.Equals("ConnStr"))//讀 {
nodeList.Attributes["value"].InnerText="寫";
}
}
domSysinfo.Save(AppDomain.CurrentDomain.BaseDirectory + "//app.config");
XML code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnStr" value="連接串" />
</appSettings>
</configuration>
首先在应用程序里添加配置文件
app.config file:
XML code<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- User application and configured property settings go here.-->
<!-- Example: <add key="settingName" value="settingValue"/> -->
<add key="ServerIP" value="127.0.0.1"/>
<add key="Server" value="test_temp"></add>
<add key="user" value="sa"></add>
<add key="password" value="sa"></add>
</appSettings>
</configuration>
数据连接字符串,可以读取到当前app.config中的数据库连接信息:
C# codestring strcon=String.Format ("packet size=4096;data source={0};persist security info=True;initial catalog={1};user id={2};password={3}",ConfigurationSettings.AppSettings["SQLserverIP"],ConfigurationSettings.AppSetting["Server"],ConfigurationSettings.AppSetting["user"],ConfigurationSettings.AppSettings["password"]);
按搂主的意思可以做一config窗体,上面需有IP,Database,user,password等组成的字符串,然后可以动态修改。
比如我们加几个textbox,对应相关数据库连接信息如下:
txtIP1.Text:ServerIP//服务器
txtSvrName.Text:Database//数据库实例(名称)
txtName1.Text:user//数据库用户
txtPassword1.Text:password//密码
如果修改连接信息,直接在textbox上修改就可以,那需要写个方法:
首先引用了
using System.Xml;
using System.Configuration;
命名空间
C# codeprivate void SaveConfig(string ConnenctionString,string strKey)
{
XmlDocument doc=new XmlDocument();
//获得配置文件的全路径
string strFileName=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes=doc.GetElementsByTagName("add");
for(int i=0;i<nodes.Count;i++)
{
//获得将当前元素的key属性
XmlAttribute att=nodes.Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value==strKey)
{
//对目标元素中的第二个属性赋值
att=nodes.Attributes["value"];
att.Value=ConnenctionString;
break;
}
}
//保存上面的修改
doc.Save(strFileName);
}
再修改保存设置时调用SaveConfig()方法:
C# codeprivate void btnOK_Click(object sender, System.EventArgs e)
{
if (txtIP1.Text=="")
{
MessageBox.Show("ServerIP is not allow null");
return ;
}
else if(txtSvrName.Text=="")
{
MessageBox.Show("DataBase is not allow null");
return ;
}
else if(txtName1.Text=="")
{
MessageBox.Show("User Name is not allow null");
return ;
}
else
{
SaveConfig(txtIP1.Text,"ServerIP");
SaveConfig(txtSvrName.Text,"Server");
SaveConfig(txtName1.Text,"user");
SaveConfig(txtPassword1.Text,"password");
MessageBox.Show("Config Sucessful!","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
this.Close();
}
注意你程序目录下,程序运行时应该是修改在当前域ConfigurationFile
bin/debug/AutoRun.exe.config