C#下.NET配置文件
来源:互联网 发布:软件项目初步设计方案 编辑:程序博客网 时间:2024/06/14 02:32
一步一步来,看看.net下的的 Configuration 或者说 Settings
走马观花
System.Configuration 命名空间中的东西是为读写应用程序的配置数据服务的。
在Windows早期,程序使用 .ini 作为配置文件,后来开始鼓励大家使用注册表,到了.NET中,又回归到使用文件,只不过这次默认是xml格式的文件。
用例子说话:
例子
一
读 程序program.exe默认配置文件 program.exe.config 中的 "appSettings" 段
二
读写 program.exe.config 中的 "appSettings" 段
三
读写 program.exe.config 中的自定义字段 "QtSection"
四
读写 roaming 文件中的自定义字段"QtSection"
五
读写 任意位置的配置文件?
例子一
- 配置文件 program.exe.config
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="first name" value="Debao" /> <add key="last name" value="Zhang" /> </appSettings></configuration>
- 程序源码 program.cs
using System.Configuration;namespace DbZhang800{ public class TestSettings { public static void Main() { foreach (string key in ConfigurationManager.AppSettings) { string val = ConfigurationManager.AppSettings[key]; Console.WriteLine("{0}: {1}", key, val); } } }}
编译运行,结果如下:
E:\> csc program.csE:\> program.exefirst name: Debaolast name: Zhang
通过 ConfigurationManager.AppSettings 我们可以很方便地读取默认配置文件中的 appSettings 段。
例子二
前面是只读的,如果需要写入怎么办?
Configuration appConf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); appConf.AppSettings.Settings.Add("Qt", "Quick"); appConf.Save();
编译运行(注,这是运行3次后的结果,每次都给Qt这个key增加了一项):
E:\> csc program.csE:\> program.exefirst name: Debaolast name: ZhangQt: Quick,Quick,Quick
此时的配置文件 program.exe.config 内容如下:
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="first name" value="Debao" /> <add key="last name" value="Zhang" /> <add key="Qt" value="Quick,Quick,Quick" /> </appSettings></configuration>
例子三
看看读写自定义字段的情况。不操作appSettings,操作一个自定义的QtSection段
- program.cs
首先创建一个ConfigurationSection的派生类
- 然后打开一个配置文件,通过该派生类进行操作
using System;using System.Configuration;namespace DbZhang800{ public sealed class QtSection : ConfigurationSection { [ConfigurationProperty("version", DefaultValue="4.4.0")] public string Version { get { return (string)this["version"]; } set { this["version"] = value; } } } public class TestSettings { public static void Main() { Configuration appConf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); QtSection qtsection; if ((qtsection = (QtSection)appConf.Sections["QtSection"]) == null) { qtsection = new QtSection(); //qtsection.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser; appConf.Sections.Add("QtSection", qtsection); } Console.WriteLine("Current: {0}", qtsection.Version); qtsection.Version = "5.0.0"; appConf.Save(ConfigurationSaveMode.Full); } }}
运行后,生成的配置文件如下:
- program.exe.config
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="QtSection" type="DbZhang800.QtSection, Program, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" /> </configSections> <QtSection version="5.0.0" /></configuration>
注意:它在configSections自动添加了我们自定义段的一些信息。
例子四
前面的配置文件和应用程序在同一个目录下,如果不同用户需要不同的配置文件怎么办呢?
前面的例子中,OpenExeConfiguration 使用的参数是 None,该参数指定不同的级别
None
只使用程序默认配置文件
PerUserRoaming
也要使用用户 roam 文件
PerUserRoamingAndLocal
也要使用用户local 文件
在前一个例子基础上,我们把 None 改为 PerUserRoaming看看,结果... 运行时:
E:\TestSettings>program.exeCurrent: 5.0.0未处理的异常: System.Configuration.ConfigurationErrorsException: 执行 !QtSection 的配置节处理程序时出错。 ---> System.InvalidOperationException: 不能编辑已锁定的 ConfigurationSection 属性。...
报出异常,无法将其写入到roaming文件,原因何在呢?
就在于我们在xml文件看到的那个 MachineToApplication 属性,该段只能写入到machine和appplication级别的配置文件,无法写入用户配置文件。
我们需要在生成这个段的时候就添加一个 MachineToRoamingUser 属性
public static void Main() { Configuration appConf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming); QtSection qtsection; if ((qtsection = (QtSection)appConf.Sections["QtSection"]) == null) { qtsection = new QtSection(); qtsection.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToRoamingUser; appConf.Sections.Add("QtSection", qtsection); } Console.WriteLine("Current: {0}", qtsection.Version); qtsection.Version = "5.0.0"; Console.WriteLine(appConf.FilePath); appConf.Save(ConfigurationSaveMode.Full); }
这样,配置可以正常写入
C:\Users\dbzhang\AppData\Roaming\DbZhang800\Program.exe_Url_foycvmvcbx4tf51hrft53z11fmpo2kto\0.0.0.0\user.config
打开其他的配置文件:
ConfigurationManager.OpenExeConfiguration(string)
- 这个东西,string 指定 exe 的路径(但不限于,该路径后会被自动添加一个 .config)
ConfigurationManager..::.OpenMachineConfiguration()
- 位于%windir%\Microsoft.NET\Framework\version\config
例子五
操作其他的配置文件
比如,我要操作当前工作目录下的 abcd.config 这个文件,那么需要使用 OpenMappedExcConfiguration:
public static void Main() { string path = System.IO.Path.Combine(Environment.CurrentDirectory, "abcd.config"); ExeConfigurationFileMap configFile = new ExeConfigurationFileMap(); configFile.ExeConfigFilename = path; Configuration appConf = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None); QtSection qtsection; if ((qtsection = (QtSection)appConf.Sections["QtSection"]) == null) { qtsection = new QtSection(); appConf.Sections.Add("QtSection", qtsection); } Console.WriteLine("Current: {0}", qtsection.Version); qtsection.Version = "5.0.0"; appConf.Save(ConfigurationSaveMode.Full); }
http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET-2-0
http://blog.davidsilvasmith.com/2011/01/storing-user-settings.html
- C#下.NET配置文件
- C#下.NET配置文件使用(一)
- C#下.NET配置文件使用(二)
- 无配置文件情况下使用Quartz.NET
- .Net Core下如何管理配置文件
- asp.net C# 获得配置文件AppSettings 的值
- .NET学习技巧:C# 配置文件读取与修改
- C#进阶ADO.NET基础二 DataSet、配置文件、SqlHelper
- C# 配置文件
- .NET配置文件
- .Net配置文件
- .NET配置文件
- .Net 配置文件
- 轻装上阵:无配置文件情况下使用Quartz.NET
- .Net下C#播放WAV,WAVE文件
- 在VS2005.net下使用C#下的XML
- 如何用c#读写配置文件(转自:http://blog.csdn.net/lanman/article/details/5287717)
- C# asp.net 中关于配置文件的配置和作用(数据库部分)
- 四种途径将HTML5应用变成android应用
- 面向对象技术和七项优点
- 一个很有意思的java代码例子
- ORA-06512 DBSNMP.BSLN_INTERNAL
- 等待队列
- C#下.NET配置文件
- centos 安装 webmin工具
- SDWebImage
- sencha-touch1.1中的时间转换函数用法
- Android中程序与Service交互的方式——交互方式
- 11gR2 ASM ORA-29701
- 孩子不想成为父母传宗接代赚取面子的工具
- 编写代码你用什么字体?
- 通过Servlet过滤器处理Web的编码问题