C# WINFORM 应用程序动态读写xml config文件,获取数
在实际项目里,我们需要用一个应用程序去连接多个数据库,有的进行测试,有的是数据库基本结构相同,数据不同, 我们不可能总去程序的连接字符串里去修改,更不能让用户去修改,所以需要动态去修改连接数据库配置信息。如果安全性可考虑的话需要对字符串加密,我这里写点简单的实现,希望大家有好的方法或意见,请执教和批评。
1 在应用程序里添加app.config
<?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="Automation_temp"></add>
<add key="user" value="sa"></add>
<add key="password" value="shan"></add>
</appSettings>
</configuration>
程序读取数据库连接,如下:
如果想把连接的信息显示出来,可以去解析字符串strcon,获取相关信息
private void Open()
...{
// open connection
if (con == null)
...{
string 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.AppSettings["Server"],ConfigurationSettings.AppSettings["user"],ConfigurationSettings.AppSettings["password"]);
con = new SqlConnection(strcon);
try
...{
con.Open();
}
catch(Exception ee)
...{
ee.ToString();
}
}
}
2 新建窗体ConfigFrm
添加4个label ,分别是:
服务器ip,Database Name,SA,password,
4个TextBox,分别是:
txtIP
txtDataBaseName
txtName
txtPwd
1个确认按钮btnOK,
3 写个方法保存修改的设置:
private void SaveConfig(string ConnenctionString,string strKey)
...{
XmlDocument doc=new XmlDocument();
//获得配置文件的全路径
string strFileName=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes=doc.getElementsByTagName_r("add");
for(int i=0;i<nodes.Count;i++)
...{
//获得将当前元素的key属性
XmlAttribute att=nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value==strKey)
...{
//对目标元素中的第二个属性赋值
att=nodes[i].Attributes["value"];
att.Value=ConnenctionString;
break;
}
}
//保存上面的修改
doc.Save(strFileName);
}
4 在确认按钮btnOK click事件:
private void btnOK_Click(object sender, System.EventArgs e)
...{
if (txtIP.Text=="")
...{
MessageBox.Show("ServerIP is not allow null");
return ;
}
else if(txtDataBaseName.Text=="")
...{
MessageBox.Show("DataBase is not allow null");
return ;
}
else if(txtName.Text=="")
...{
MessageBox.Show("User Name is not allow null");
return ;
}
else
...{
SaveConfig(txtIP.Text,"ServerIP");
SaveConfig(txtDataBaseName.Text,"Server");
SaveConfig(txtName.Text,"user");
SaveConfig(txtPassword.Text,"password");
MessageBox.Show("Config Sucessful!","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
this.Close();
}
在应用程序当前目录下,程序动态加载的是 /bin/debug/test.exe.config信息,从而实现了动态读写xml文件,去获取
数据库连接。
在实际项目里,我们需要用一个应用程序去连接多个数据库,有的进行测试,有的是数据库基本结构相同,数据不同, 我们不可能总去程序的连接字符串里去修改,更不能让用户去修改,所以需要动态去修改连接数据库配置信息。如果安全性可考虑的话需要对字符串加密,我这里写点简单的实现,希望大家有好的方法或意见,请执教和批评。
1 在应用程序里添加app.config
<?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="Automation_temp"></add>
<add key="user" value="sa"></add>
<add key="password" value="shan"></add>
</appSettings>
</configuration>
程序读取数据库连接,如下:
如果想把连接的信息显示出来,可以去解析字符串strcon,获取相关信息
private void Open()
...{
// open connection
if (con == null)
...{
string 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.AppSettings["Server"],ConfigurationSettings.AppSettings["user"],ConfigurationSettings.AppSettings["password"]);
con = new SqlConnection(strcon);
try
...{
con.Open();
}
catch(Exception ee)
...{
ee.ToString();
}
}
}
2 新建窗体ConfigFrm
添加4个label ,分别是:
服务器ip,Database Name,SA,password,
4个TextBox,分别是:
txtIP
txtDataBaseName
txtName
txtPwd
1个确认按钮btnOK,
3 写个方法保存修改的设置:
private void SaveConfig(string ConnenctionString,string strKey)
...{
XmlDocument doc=new XmlDocument();
//获得配置文件的全路径
string strFileName=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes=doc.getElementsByTagName_r("add");
for(int i=0;i<nodes.Count;i++)
...{
//获得将当前元素的key属性
XmlAttribute att=nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value==strKey)
...{
//对目标元素中的第二个属性赋值
att=nodes[i].Attributes["value"];
att.Value=ConnenctionString;
break;
}
}
//保存上面的修改
doc.Save(strFileName);
}
4 在确认按钮btnOK click事件:
private void btnOK_Click(object sender, System.EventArgs e)
...{
if (txtIP.Text=="")
...{
MessageBox.Show("ServerIP is not allow null");
return ;
}
else if(txtDataBaseName.Text=="")
...{
MessageBox.Show("DataBase is not allow null");
return ;
}
else if(txtName.Text=="")
...{
MessageBox.Show("User Name is not allow null");
return ;
}
else
...{
SaveConfig(txtIP.Text,"ServerIP");
SaveConfig(txtDataBaseName.Text,"Server");
SaveConfig(txtName.Text,"user");
SaveConfig(txtPassword.Text,"password");
MessageBox.Show("Config Sucessful!","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
this.Close();
}
在应用程序当前目录下,程序动态加载的是 /bin/debug/test.exe.config信息,从而实现了动态读写xml文件,去获取
数据库连接。
C# WINFORM 应用程序动态读写xml config文件,获取数的更多相关文章
- C#中动态读写App.config配置文件
转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...
- 通过源码了解ASP.NET MVC 几种Filter的执行过程 在Winform中菜单动态添加“最近使用文件”
通过源码了解ASP.NET MVC 几种Filter的执行过程 一.前言 之前也阅读过MVC的源码,并了解过各个模块的运行原理和执行过程,但都没有形成文章(所以也忘得特别快),总感觉分析源码是大神 ...
- 【C#】【WPF】如何读写app.config文件
WPF生成的项目中会有.exe.config.一般是系统默认配置的 格式是xml格式,C#的项目可以直接读写这些文件.方法代码如下. public static string GetConnectio ...
- OpenCV FileStorage类读写XML/YML文件
本文转自:http://www.cnblogs.com/summerRQ/articles/2524560.html 在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件, ...
- 关于读写APP.config文件能读却写不了的问题
今天要求用winform写一个窗口用来读写一个App.config,要对 <appSettings>里面的add key和value进行添加和修改.要实现的效果图如下: -------- ...
- Winform读写App.config文件以及重启程序
//重启主程序 //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Locatio ...
- 建立连接ALM的xml config文件
我就不贴所有的了,如果有谁想要所有源码和应用程序,可以密我 这里我贴下如何在第一次运行的时候自动建立一个ALMConfig的xml文件 private static void CreateALMCon ...
- 在控制台程序中,添加config文件
一.右击类库 → 添加 → 新建项 → 应用程序配置文件(或者选择一个XML文件,然后将名字改成XXX.config),内容如下: <?xml version="1.0" e ...
- 在Winform中菜单动态添加“最近使用文件”
最近在做文件处理系统中,要把最近打开文件显示出来,方便用户使用.网上资料有说,去遍历“C:\Documents and Settings\Administrator\Recent”下的最近文档本.文主 ...
随机推荐
- Socket网络编程基本介绍
一,socket的起源 socket一词的起源 在组网领域的首次使用是在1970年2月12日发布的文献IETF RFC33中发现的, 撰写者为Stephen Carr.Steve Crocker和Vi ...
- Python机器学习笔记 使用sklearn做特征工程和数据挖掘
特征处理是特征工程的核心部分,特征工程是数据分析中最耗时间和精力的一部分工作,它不像算法和模型那样式确定的步骤,更多的是工程上的经验和权衡,因此没有统一的方法,但是sklearn提供了较为完整的特征处 ...
- python装饰器1:函数装饰器详解
装饰器1:函数装饰器 装饰器2:类装饰器 装饰器3:进阶 先混个眼熟 谁可以作为装饰器(可以将谁编写成装饰器): 函数 方法 实现了__call__的可调用类 装饰器可以去装饰谁(谁可以被装饰): 函 ...
- 分享一个爬取HUST(哈理工)学生成绩的Python程序(OCR自动识别验证码)
Python版本:3.5.2 日期:2018/1/21 __Author__ = "Lance#" # -*- coding = utf-8 -*- from urllib imp ...
- MVC学习之路【小补充】
1]:在js中使用ViewBag 需要添加“”,否则程序报错,无法正常运行 .例如:正确格式 var ss = "@ViewBag.ts"
- 【转载】 IIS服务器防盗链设置
在实际运行的服务器环境中,我们自己网站中的资源一般不希望被外部网站引用,被外部网站引用IIS网站中的资源文件,一是会加重了服务器的负担,二是占用了你自己服务器的外网带宽资源,因此我们希望防止盗链这种情 ...
- mysql 数据库的备份与还原 at winows
把cmd的当前目录切换到mysql安装目录; 备份数据库world mysqldump -u root -p world < c:\all.sql 导入数据库 新建schema world 常 ...
- JQuery官方学习资料(译):JQuery对象
每当创建一个新的元素(或者选择一个已经存在的元素)时,JQuery将返回一个元素的集合.大部分的开发人员新接触JQuery的时候,都把这个集合当做数组.这个集合中的DOM元素有从零开始的索引, ...
- H5 五子棋源码
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 12 Linux Which Command, Whatis Command, Whereis Command Examples
This Linux tutorial will explain the three "W" commands. The three "W"s are what ...