使用ConfigurationManager类 读写配置文件app.config,以下为代码:
using System;
using System.Configuration;
static class Program
{
static void Main()
{
showConfig();
UpdateAppSettings();
showConfig();
Console.ReadKey(true);
}
private static void showConfig()
{
string = ConfigurationManager.AppSettings["Directory"];
Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "\n");
}
///
/// UpdateAppSettings
///
public static void UpdateAppSettings()
{
// Get the configuration file.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");
config.AppSettings.Settings["Directory"].Value = "tset";
// Save the configuration file.
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
}
using System;
using System.Configuration;
static class Program
{
static void Main()
{
showConfig();
UpdateAppSettings();
showConfig();
Console.ReadKey(true);
}
private static void showConfig()
{
string = ConfigurationManager.AppSettings["Directory"];
Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "\n");
}
///
/// UpdateAppSettings
///
public static void UpdateAppSettings()
{
// Get the configuration file.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");
config.AppSettings.Settings["Directory"].Value = "tset";
// Save the configuration file.
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
}
app.config内容:
view plaincopy to clipboardprint?
代码结果:app.config只能作为初始化的定义,工程生成后运行程序集名称.exe 修改生成后的 程序集名称.exe.Config文件
一开始调试时看到控制结果是想要的结果,但看app.config配置文件内容没变(vs2008 F5调试模式下是修改 程序集名称.vshost.exe.config配置文件)还以为是代码有问题,网上搜,也有人碰过到此现像,原来是自己没有理解到MSDN的说明。(还是有文化差异啊)如:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");
查看config.filePath值,即了解明白了。
4.0的类库: http://msdn.microsoft.com/en-us/library/ms134265(v=VS.100).aspx
注意:此类是.net2.0后新增。
必须要先在工程里添加system.configuration.dll程序集的引用。
(在解决方案管理器中右键点击工程名称,在右键菜单中选择添加引用,.net->组件名称->下即可找到)
添加引用后可以使用System.Configuration空间下的ConfigurationManager类.
引用资源:
使用java.util.Properties类读写配置文件
J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...
System.ConfigurationManager类用于对配置文件的读取
http://blog.csdn.net/ligenyingsr/article/details/54095986 System.ConfigurationManager类用于对配置文件的读取.其具有 ...
Java读写配置文件——Properties类的简要使用笔记
任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外. 在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XM ...
C读写配置文件
在项目开发中,经常需要读取应用配置文件的初始化参数,用于应用在启动前进行一些初始化配置.比如:Eclipse,参数项包含主题.字体大小.颜色.Jdk安装位置.自动提示等.Eclispe配置的文件格式是 ...
python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
引用了System.Configuration命名空间,却找不到ConfigurationManager类
用ConfigurationManager类来读取应用程序配置文件的信息时,提示:System.Configuration命名空间下找不到ConfigurationManager类 查过资料后得知:要 ...
python-ConfigParser模块【读写配置文件】
对python 读写配置文件的具体方案的介绍 1,函数介绍 import configParser 如果Configparser无效将导入的configParser 的C小写 1.1.读取配置文件 - ...
ConfigParser 读写配置文件
一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2 ...
python利用ConfigParser读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法非常简单. 配置文件的格式是: []包含的叫section, section 下有option=value这样的键值 ...
随机推荐
Excel2013 基本用法(下)
排序 按行/列排序 选择单元格,点击"排序"(点击之后自动全选) 点击选项,弹出"排序选项",根据自己需要选择按行排序/按列排序. 输入排序条件. 确定. 复杂 ...
C#WebForm内置对象
内置对象: Response对象:响应请求Response.Write("");Respons ...
20150625_Andriod_02_ListView2_多条目显示_选中
android listview 参考地址: http://www.cnblogs.com/zhengbeibei/archive/2013/05/14/3078805.html http://xy ...
poj 1329 Circle Through Three Points(求圆心+输出)
题目链接:http://poj.org/problem?id=1329 输出很蛋疼,要考虑系数为0,输出也不同 #include #include
获取字符宽度:并非自适应。coretext去计算
获取字符宽度:并非自适应.coretext去计算 UniChar ch = [ns_str characterAtIndex:0]; CGGlyph glyph = 0; CTFontGetGlyph ...
Morphia采用
Morphia配合mongoDB喜欢Hibernate关系型数据库.也能够想到Morphia于MongoDB它调用上述的基础上,mongoDB语法. 于java直接通话Morphia的api它会变得更 ...
HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
共享MFC每周时间选择控件代码
自己写的周时间选择控件,原理就是在Static上用GDI画图. 支持选择每周内每一天内的任意时间段,可以任意拖动修改时间段,任意合并时间段 效果如下图: VS2012代码下载:https://gith ...