config文件更改

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值