C# winform把新值写入app.config文件
来源:互联网 发布:w7 mysql更改密码 编辑:程序博客网 时间:2024/05/19 05:32
最简单的winform配置文件app.config如下:
<?xml version="1.0" encoding="utf-8"?><configuration> <appSettings> <add key="ftpServer" value="192.168.123.456" /> <add key="ftpUserName" value="hahaha" /> <add key="password" value="123456" /> <add key="localDefaultDirectory" value="E:/" /> <add key="remoteDefaultDirectory" value="/" /> <add key="nameList" value="123 456 789 10 11 12" /> </appSettings></configuration>
网上找的只能暂时把新设置的值加载进去,典型处理方法如下:
private void AccessAppSettings(){ //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //根据Key读取<add>元素的Value string name = config.AppSettings.Settings["name"].Value; //写入<add>元素的Value config.AppSettings.Settings["name"].Value = "fx163"; //增加<add>元素 config.AppSettings.Settings.Add("url", "http://www.fx163.net"); //删除<add>元素 config.AppSettings.Settings.Remove("name"); //一定要记得保存,写不带参数的config.Save()也可以 config.Save(ConfigurationSaveMode.Modified); //刷新,否则程序读取的还是之前的值(可能已装入内存) System.Configuration.ConfigurationManager.RefreshSection("appSettings");}
但是回头去看app.config和exe.config,这两个xml文件都没变。这里提供一个直接把新值写入xml的方法:
private void butOk_Click(object sender, EventArgs e) { XmlDocument doc01 = new XmlDocument(); XmlDocument doc02 = new XmlDocument(); //获得配置文件的全路径 string strFileName01 = AppDomain.CurrentDomain.BaseDirectory + "Winftp.exe.config"; string path = AppDomain.CurrentDomain.BaseDirectory; DirectoryInfo dir = Directory.GetParent(path); string strFileName02 = dir.Parent.Parent.FullName + "\\app.config"; doc01.Load(strFileName01); doc02.Load(strFileName02);//'F:\work\Win7FTP\Win7FTP\Win7FTP\bin\Debugapp.config'. //找出名称为“add”的所有元素 XmlNodeList nodes01 = doc01.GetElementsByTagName("add"); XmlNodeList nodes02 = doc02.GetElementsByTagName("add"); for (int i = 0; i < nodes01.Count; i++) { //获得将当前元素的key属性 XmlAttribute att = nodes01[i].Attributes["key"]; //根据元素的第一个属性来判断当前的元素是不是目标元素 if (att.Value == "ftpServer") { //对目标元素中的第二个属性赋值 att = nodes01[i].Attributes["value"]; att.Value = this.txtHostName.Text.ToString(); break; } } for (int i = 0; i < nodes02.Count; i++) { //获得将当前元素的key属性 XmlAttribute att = nodes02[i].Attributes["key"]; //根据元素的第一个属性来判断当前的元素是不是目标元素 if (att.Value == "ftpServer") { //对目标元素中的第二个属性赋值 att = nodes02[i].Attributes["value"]; att.Value = this.txtHostName.Text.ToString(); break; } } //保存上面的修改 doc01.Save(strFileName01); doc01.Save(strFileName02); System.Configuration.ConfigurationManager.RefreshSection("appSettings"); }
阅读全文
0 0
- C# winform把新值写入app.config文件
- C# Winform 修改app.config文件
- C# Winform 修改app.config文件
- c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改
- winform中 如何读取Excel文件,之后再把它写入一个新的Excel文件
- C#资料库写入路径写在app.config
- C# Winform App.config加密 aspnet_regiis
- C# winform app.config 发布时不见了?
- C# winForm读取配置文件 App.config
- winform中读写App.config文件
- C#Winform动态读取App.Config文件
- c# 把时间写入文件
- C#读取app.config文件
- C#配置App.config文件
- C# 读写app.config文件
- 把WINFORM嵌入IE如何带上app.config?
- winform项目的app.config和exe.config文件
- winform项目的app.config和exe.config文件
- 利用Python创建链接mysql数据库个性化对象
- spark partitioner使用技巧
- 二叉树的最大深度
- JSP的7个动作指令
- jmeter解决响应数据乱码问题
- C# winform把新值写入app.config文件
- (六)Interacting with Other Apps与其他应用进行交互
- 什么是事件委托?
- 红黑树插入算法调整的一些原则
- 欢迎使用CSDN-markdown编辑器
- [数据库与SQL]
- 学习嵌入式Linux系统开发基础 第五课(Linux服务器配置)
- 数据库-sql语法例子
- svn应用中的注意事项