本文主要是介绍C# winform存储配置文件之App.config文件读写,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
有时候一些用户的配置需要写入在本地,不能每次程序启动都让用户重新设置一下吧。
下面先说基本用法:
1、创建winform项目之后自动会生成App.config文件,如果默认没有,就对项目右键-新建项-APP配置文件
这个文件创建后自动就有的,如果没有就新建一个,我们可以往里面添加需要的数据:
<?xml version="1.0" encoding="utf-8" ?>
<configuration><appSettings><add key="key1" value="hello"/><add key="key2" value="world!"/></appSettings>
</configuration>
里面的appSettings节点和子节点就是我添加上去的
2、读写
项目的引用增加System.Configuration,然后在cs文件引入System.Configuration
然后这样就可以读到数据了:
string key1 = ConfigurationManager.AppSettings["key1"];Debug.WriteLine("key1=" + key1);
这样修改数据:
ConfigurationManager.AppSettings.Set("key1","new hello!");Debug.WriteLine("new-key1=" + ConfigurationManager.AppSettings["key1"]);
一切看起来都非常简单,没有什么问题,打印出来显示都是正确的。
实际上他设置的时候并没有真正写入xml,而是修改了内存中的值而已!
3、正确的做法应该是按照对待一个普通的xml来进行读写操作,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;namespace TestConfig
{class ConfigSettings{private ConfigSettings() { }public static string ReadSetting(string key) {//不建议通过这种自带的方式进行读取;如果手动修改了配置文件,则不会第二次读取的时候,依旧是内存中的值。可以通过XML方式进行读取。//return ConfigurationSettings.AppSettings[key];XmlDocument doc = loadConfigDocument();XmlNode node = doc.SelectSingleNode("//appSettings");if (node == null) throw new InvalidOperationException("appSettings section not found in config file.");try{XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));if (elem != null) {return elem.GetAttribute("value");}}catch {throw;}return "";}public static void WriteSetting(string key, string value) {XmlDocument doc = loadConfigDocument();XmlNode node = doc.SelectSingleNode("//appSettings");if (node == null)throw new InvalidOperationException("appSettings section not found in config file.");try{XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));if (elem != null){elem.SetAttribute("value", value);}else{elem = doc.CreateElement("add");elem.SetAttribute("key", key);elem.SetAttribute("value", value);node.AppendChild(elem);}doc.Save(getConfigFilePath());}catch {throw;}}public static void RemoveSetting(string key) {XmlDocument doc = loadConfigDocument();XmlNode node = doc.SelectSingleNode("//appSettings");try{if (node == null)throw new InvalidOperationException("appSettings section not found in config file.");else{node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));doc.Save(getConfigFilePath());}}catch(NullReferenceException e) {throw new Exception(string.Format("The key {0} does not exist.", key), e);}}private static XmlDocument loadConfigDocument(){XmlDocument doc = null;try{doc = new XmlDocument();doc.Load(getConfigFilePath());return doc;}catch (System.IO.FileNotFoundException e){throw new Exception("No configuration file found.", e);}}private static string getConfigFilePath(){return Assembly.GetExecutingAssembly().Location + ".config";}}
}
这篇关于C# winform存储配置文件之App.config文件读写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!