C#读取和写入配置文件
使用.Net2.0中的ConfigurationManager可以方便的实现对配置app.config的读取和写入。
ConfigurationManager默认没有自动载入项目,使用前必须手动添加,方法如下:
项目->引用->添加引用->选择System.configuration
1.使用ConfigurationManager读配置文件
我们可以将简单的配置内容放到app.config中的appSettings节点中如下:
<appSettings>
<add key="GPRS.Port1" value="5002"/>
<add key="GPRS.Port2" value="5003"/>
<add key="GPRS.Port3" value="5004"/>
</appSettings>
然后在程序中使用ConfigurationManager.AppSettings["GPRS.Port1"]来读取GPRS.Port1的值
2.使用ConfigurationManager写配置文件
如何想要把修改过的GPRS.Port1的值存放回app.config,可以使用下面的代码
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["GPRS.Port1"].Value = “xxxxx”;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
C#读取和写入配置文件的更多相关文章
- Python Configparser模块读取、写入配置文件
写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...
- Python 读取写入配置文件 —— ConfigParser
Python 读取写入配置文件 —— ConfigParser Python 读取写入配置文件很方便,可使用内置的 configparser 模块:可查看源码,如博主本机地址: “C:/python2 ...
- properties配置文件的读取和写入
/** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...
- Java学习-023-Properties 类 XML 配置文件读取及写入源代码
之前的几篇 Properties 文章已经讲述过了 Java 配置文件类 Properties 的基本用法,查看 JDK 的帮助文档时,也可看到在 Properties 类中还有两个方法 loadFr ...
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- 关于java读取和写入properties配置文件的内容
一般通过使用流的方式进行读取 代码示例如下: package com.zznode.transmit.util; import java.io.FileInputStream; import java ...
- 对Java配置文件Properties的读取、写入与更新操作
http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties 对Jav ...
- 实现对Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- Winform中自定义xml配置文件后对节点进行读取与写入
场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 ...
随机推荐
- FAQ: Automatic Statistics Collection (文档 ID 1233203.1)
In this Document Purpose Questions and Answers What kind of statistics do the Automated tasks ...
- OC基础(17)
ARC基本概念 ARC快速入门 ARC下的内存管理 ARC和MRC兼容和转换 *:first-child { margin-top: 0 !important; } body > *:last- ...
- 《Code Complete》ch.14 组织直线型的代码
WHAT? 最简单的控制流:即按照先后顺序放置语句与语句块 WHY? 尽管组织直线型的代码是一个简单的任务,但代码结构上的一些微妙之处还是会对代码质量.正确性.可读性和可维护性带来影响 HOW? 必须 ...
- 怎样用CODESOFT打印连续的条码标签?
在实际工作中,经常会用CODESOFT条 码打印软件来实现打印连续的条码标签,将这些标签按递增或递减等方式连续打印.这样设置可大大提高用户的工作效率.实现在CODESOFT 2015打印连续条码标签, ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- delphi调用web service出现 Unable to retrieve the URL endpoint for Service/Port .....
delphi调用web service出现 Unable to retrieve the URL endpoint for Service/Port, 错误截图如下 查了很长时间, 发现在DataM ...
- -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
在有全屏侧滑的情况下,页面上有个slider需要左右滑动的时候,经常在滑动slider的时候页面也跟着滑动 解决办法一:关闭当前页面的全屏侧滑,开启系统侧滑 self ...
- My Rules of Information
http://www.infotoday.com/searcher/jan02/block.htm I often suggested to students that information is ...
- 详解android.mk-2016.01.18
1 Android.mk作用 当使用JNI开发时,我们需要创建一个native工程,Android.mk就是一个makefile的配置文件,帮助我们把编写的C/C++代码编译成动态或者静态的链接库. ...
- Java 对字符反转操作。
//把一段字符串反转后大小写互换位置 public class test_demo { public static void main(String[] args)throws Exception { ...