Winform 读取 指定\另一个\其他\任意 配置文件
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = @"F:\App1.config"; ;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
string connstr = config.ConnectionStrings.ConnectionStrings["connStr"].ConnectionString;
MessageBox.Show(connstr);
string key = config.AppSettings.Settings["key"].Value;
MessageBox.Show(key);
How To Read/Write Another App.Config File
To open another App.Config file you need to create an instance of ExeConfigurationFileMap.
The purpose of this class is not that obvious but we can use it to open
another file. Once you have learned this little trick the rest is easy.
Here is a little example that does open an file by specifying it's
name.
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"ConfigTest.exe.config"; // relative path names possible
// Open another config file
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
// read/write from it as usual
ConfigurationSection mySection = config.GetSection("mySection");
config.SectionGroups.Clear(); // make changes to it
config.Save(ConfigurationSaveMode.Full); // Save changes
The Microsoft Enterprise Library way
has a shorthand utility class for this. It is the
FileConfigurationSource which does hide those strange things. Tom Hollander has a nice post explaining this already so I will not repeat the same at my blog.
Another Way to read/write configuration values
A
more advanced way to store our settings is to create our own
ConfigurationSection. This makes our configuration values
distinguishable from other configuration values inside the App.config
file. It is a little more complicated since you have to write your own
class which content is de/serialized to the App.config file. I am going
to show you at first the config file and explain then what code you need
to write to read/save these settings to your application configuration
file.
App.Config (Taken from the Enterprise Library Configuration Migration QuickStart Sample)
<configuration>
<configSections>
<section name="EditorSettings" type="ConfigurationMigrationQuickStart.EditorFontData,
ConfigurationMigrationQuickStart, Version=1.1.0.0, Culture=neutral,
PublicKeyToken=null" />
</configSections>
<EditorSettings name="Verdana" size="24" style="2" />
</configuration>
Most App.config files which contain config data have a <configSections> element where many <section> are defined. The name attribute of an section (in this example "EditorSettings") tells the config system that the class ConfigurationMigrationQuickStart.EditorFontData is responsible for the ser/deserialization of the node <EditorSettings>. The EditorFontData class derives from the ConfigurationSection class and uses the ConfigurationProperty attribute to create a mapping between the properties to de/serialize and the attribute names in names in the App.Config file.
using System.Text;
using System.Configuration;
public class EditorFontData : ConfigurationSection
{
public EditorFontData()
{
}
[ConfigurationProperty("name")]
public string Name
{
get { return (string)this["name"]; }
set{ this["name"] = value; }
}
[ConfigurationProperty("size")]
public float Size
{
get{ return (float)this["size"]; }
set{ this["size"] = value; }
}
[ConfigurationProperty("style")]
public int Style
{
get { return (int)this["style"]; }
set{ this["style"] = value; }
}
}
To access an EditorFontData instance with values from your config file you only need to call
EditorFontData configData = ConfigurationManager.GetSection("EditorSettings") as EditorFontData;
Please note that the System.Configuration.ConfigurationManager
returns only objects with read only properties. Subsequent calls to
GetSection use the cached instance inside the ConfigurationManager. This
constraint requires you to create every time a new instance of you e.g.
EditorFontData object if you want to write to the App.config file. You
even cannot add an object with the same name twice to the
System.Configuration.Configuration object. Now comes the fun part: To
edit an existing App.config file you have to remove your config object
and then add a new object instance to the Configuration object. Only
then the Save will succeed.
EditorFontData configData = new EditorFontData();
configData.Name = "Arial";
configData.Size = 20;
configData.Style = 2;
// Write the new configuration data to the XML file
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.Sections.Remove("EditorSettings");
config.Sections.Add("EditorSettings", configData);
config.Save();
To get your hands on the System.Configuration.Configuration
object you have to open your App.Config file. The .NET config mechanism
supports setting inheritance from the Machine.config from which all
settings are inherited. Next comes the App.Config file which is selected
by the ConfigurationUserLevel.None file.
http://geekswithblogs.net/akraus1/articles/64871.aspx
Winform 读取 指定\另一个\其他\任意 配置文件的更多相关文章
- WinForm读取指定的config文件的内容
config文件的使用 一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C# ...
- C#读取指定路径下的Config配置文件
ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @"F:\App1. ...
- 读取指定文件夹下的全部文件,可通过正则进行过滤,返回文件路径数组 -- 基于node的一个函数
var fs = require('fs'); // 模板文件夹路径 var templateDirectory = '../src'; //相对于当前文件的相对路径 //var templateDi ...
- Shell 命令行,实现一个获取任意位数的随机密码的脚本
Shell 命令行,实现一个获取任意位数的随机密码的脚本 每次我们想要获得一个密码的时候都很头疼,于是我之前自己用nodejs写了一个 Shell 脚本.这两天在学习 bash Shell 所以,想用 ...
- 将所有程序设置XML集中到一个单独XML配置文件的方法:使用appSettings元素的configSource元素
在.NET程序中,程序的配置文件默认都会放在程序根目录下的web.config(网站程序)或App.config(控制台等程序),在该配置文件中可以定义若干程序配置信息. 在此以网站程序为例,首先将下 ...
- iOS案例:读取指定txt文件,并把文件中的内容输出出来
用到的是NSString中的initWithContentsOfFile: encoding方法 // // main.m // 读取指定文件并输出内容 // // Created by Apple ...
- Java读取WEB-INF目录下的properties配置文件
如何在Java代码中读取WEB-INF目录下的properties配置文件,下文给出了一个解决方案. 我们习惯将一些配置信息写在配置文件中,比如将数据库的配置信息URL.User和Password写在 ...
- java web实现读取指定盘符下的图像(二)
之前写了一篇文章是关于如何读取指定盘符下的图片,虽然功能可以实现,但是使用的是I/O流的方式,效率不高.现在发现还有一个更好的办法,使用也更加的方便. 我们知道,当我们的图片是放在tomcat下web ...
- 写一个简单的配置文件和日志管理(shell)
最近在做一个Linux系统方案的设计,写了一个之前升级服务程序的配置和日志管理. 共4个文件,服务端一个UpdateServer.conf配置文件和一个UpdateServer脚本,客户端一个Upda ...
随机推荐
- 7.SSRF漏洞绕过IP限制
绕过SSRF过滤的几种方法 下文出现的192.168.0.1,10.0.0.1全部为服务器端的内网地址. 1.更改IP地址写法 一些开发者会通过对传过来的URL参数进行正则匹配的方式来过滤掉内网IP, ...
- 洛谷P1932 A+B A-B A*B A/B A%B Problem
P1932 A+B A-B A*B A/B A%B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 由于数据有修改,减法运算结果可能带负号! 输入输出格式 输入格式 ...
- 剑指Offer的学习笔记(C#篇)-- 旋转数组的最小数字
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...
- 表单和css
表单和CSS 一.==表单== 1. form表单本身 <form name="myform" action="#" method="get&q ...
- Jmeter性能测试-----参数化方法CSVRead函数
Jmeter里面参数化的方法有很多,大家可以结合自己的项目情况来使用哪种方式来调用测试 数据. 下面我给大家介绍下Jmeter里CSVRead函数来获取参数的方法: 我这里已去到直播间发表评论为例(这 ...
- POJ1010 Stamps
题目来源:http://poj.org/problem?id=1010 题目大意: 某邮局要设计新的邮资管理软件,依据顾客的需要和现有的面值给顾客分派邮票. 该邮局有很多顾客是集邮爱好者.这些人希望得 ...
- poj2186-Popular Cows(强连通分支)
有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表2欢迎1 ...
- MYSQL性能优化之Mysql数据库监控
监控对象 数据库可用性监控数据库进程或是端口存在并不意味着数据库就是可用的 也就是说登陆服务器,并且能正确执行mysql命令 数据库性能 QPS和DPS并发线程数量(同时执行sql语句的数量,不是连接 ...
- calendar 类 用法
add()和roll()区别 一.Calendar 月份从 0-11,要表示8月,应该传入7 . 二.set() 会自动转换为合法的日期,如 set(1999,8,31) 表示的是1999-09-3 ...
- 一般的linux系统默认安装的vim是精简版
一般的linux系统默认安装的vim是精简版(vim-tiny),所以不能配置语法检查等属性或获取在线帮助.需要安装vim-x:x.x.x,vim-common,vim-runtime. :synta ...