【C#】【WPF】如何读写app.config文件
WPF生成的项目中会有.exe.config。一般是系统默认配置的
格式是xml格式,C#的项目可以直接读写这些文件。方法代码如下。
public static string GetConnectionStringsConfig(string connectionName)
{
string file = System.Windows.Forms.Application.ExecutablePath;
System.Configuration.Configuration sysconfig = ConfigurationManager.OpenExeConfiguration(file);
string connectionString =
sysconfig.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();
return connectionString;
}
public static void UpdateConnectionStringsConfig(string newName, string newConString)
{
string file = System.Windows.Forms.Application.ExecutablePath;
Configuration sysconfig = ConfigurationManager.OpenExeConfiguration(file);
bool exist = false;
if (sysconfig.ConnectionStrings.ConnectionStrings[newName] != null)
{
exist = true;
}
if (exist)
{
sysconfig.ConnectionStrings.ConnectionStrings.Remove(newName);
}
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName, newConString);
sysconfig.ConnectionStrings.ConnectionStrings.Add(mySettings);
sysconfig.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("ConnectionStrings");
}
以上方法可以直接向配置文件中动态写入。
还有一种方法是使用Key值的config读写
app.config的配置文件如下
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Language" value="Chinese" />
<add key="DefaultConfigPath" value="" />
<add key="DBFilePath" value="" />
</appSettings>
</configuration>
读配置文件的方法很简单,代码如下
language = ConfigurationManager.AppSettings[Options.Language];
defaultConfigPath = ConfigurationManager.AppSettings[Options.DefaultConfigPath];
dbFilePath = ConfigurationManager.AppSettings[Options.DBFilePath];
写入配置文件的方法也很简单,方法如下
public static void WriteOptions(string keyName, string newValue)
{
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings[keyName].Value = newValue;
cfa.Save();
}
调用方法完成写入指定Key值的配置文件。
这种方法仅仅在配置文件中存在指定Key值的时候可以写入Value的值。也就是修改指定Key的对应Value的值。
当然对应还有删除和添加的方法如下
public static void WriteOptions(string keyName, string newValue)
{
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//删除
cfa.AppSettings.Settings.Remove(KeyName);
//添加
cfa.AppSettings.Settings.Add(KeyName,newValue);
cfa.Save();
}
【C#】【WPF】如何读写app.config文件的更多相关文章
- WPF程序中App.Config文件的读与写
WPF程序中的App.Config文件是我们应用程序中经常使用的一种配置文件,System.Configuration.dll文件中提供了大量的读写的配置,所以它是一种高效的程序配置方式,那么今天我就 ...
- 关于读写APP.config文件能读却写不了的问题
今天要求用winform写一个窗口用来读写一个App.config,要对 <appSettings>里面的add key和value进行添加和修改.要实现的效果图如下: -------- ...
- Winform读写App.config文件以及重启程序
//重启主程序 //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Locatio ...
- WPF C#之读取并修改App.config文件
原文:WPF C#之读取并修改App.config文件 简单介绍App.config App.config文件一般是存放数据库连接字符串的. 下面来简单介绍一下App.config文件的修改和更新. ...
- WPF应用App.Config文件的保存路径
App.Config文件有更改后,自动会保存到以下路径: C:\Users\你的系统用户名\AppData\Local\你的应用名\
- C# 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- C# 读写App.config
Jul142013 [C#] 读写App.config配置文件的方法 作者:xieyc 发布:2013-07-14 17:29 字符数:3433 分类:编程 阅读: 39,139 次 ...
- C#中动态读写App.config配置文件
转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...
- C#项目实例中读取并修改App.config文件
C#项目是指一系列独特的.复杂的并相互关联的活动,这些活动有着一个明确的目标或目的,必须在特定的时间.预算.资源限定内,依据规范完成.项目参数包括项目范围.质量.成本.时间.资源. 1. 向C#项目实 ...
随机推荐
- ios开发网络学习六:设置队列请求与RunLoop
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- Android自定义组件系列【2】——Scroller类
在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友可以先看<自定义View及ViewGroup> scrollTo和scrollBy虽然实现 ...
- C++对象模型——对象成员的效率 (Object Member Efficiency)(第三章)
3.5 对象成员的效率 (Object Mem ber Efficiency) 以下某个測试,目的在測试聚合(aggregation).封装(encapsulation),以及继承(Inheritan ...
- MFC 之 OnClose 与 OnCancel
在一个对话框其中.按下esc键 与 上面的红叉,默认都是调用OnCancel()函数. 如今我要在按下esc键盘后不关闭程序而是弹出一个对话框,可是这个时候点击红叉 程序也不会关闭,为了解决问题.我 ...
- [Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName
First time dealing with Reactive form might be a little bit hard to understand. I have used Angular- ...
- DesignPattern_Java:SingletonPattern
单例模式 SingletonPattern Ensure a class has only one instance,and provide a global point of access to i ...
- [ES2016] Check if an array contains an item using Array.prototype.includes
We often want to check if an array includes a specific item. It's been common to do this with the Ar ...
- 灵活使用Excel可能会提高Java代码编写效率
使用Java操作数据时,当表字段太多时,书写实体类和进行实体类对象操作时都是一个繁重且易错的工作,光靠复制粘贴快捷键已不能满足负责的操作. 首先,说一下,就是在Eclipse中的快捷键,小写:ctrl ...
- 一起学Python:多线程-共享全局变量
多线程-共享全局变量 from threading import Thread import time g_num = 100 def work1(): global g_num for i in r ...
- idea-环境配置
显示行号 Settings->Editor->Appearance标签项,勾选Show line numbers 关闭导航 在idea14版本中,上面有个代码导航,show breadcr ...