【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#项目实 ...
随机推荐
- valgrind,arm-linux交叉编译
1. 下载及解压valgrind-3.9.0 2.CC=/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/bin/arm-hisiv200-linux ...
- jquery ajax跨域 thinkphp getjson
jquery中post的应该是不能跨域,网上说get的可以跨域,但是我试了一下也不行,然后就进行最后的拼搏getjson,结果成功,哈哈 js处写作 $.getJSON( "/index.p ...
- 【t009】最大矩形面积
Time Limit: 2 second Memory Limit: 32 MB [问题描述] 在x轴上水平放置着N个矩形,每个矩形都有相同的宽度,但是它们的高度并不相同. 比如,图1包含的矩形的高分 ...
- UUIDUtils工具类
原理是根据自身的操作系统和电脑硬件生成的一个32位的随机字符串,如果是一台电脑使用的话,一年不会重复,经过java之后貌似编程了36位,多了4个"-",下面是代码实现: impor ...
- 【59.49%】【codeforces 554B】Ohana Cleans Up
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Vue] Parent and Child component communcation
By building components, you can extend basic HTML elements and reuse encapsulated code. Most options ...
- [Angular] Observable.catch error handling in Angular
import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/opera ...
- sql for xml 还有一种写法(採用 tag 与 union all,简洁易懂)
sql for xml 还有一种写法(採用 tag 与 union all,简洁易懂) 測试环境:sql 08, 08 R2, 2010, 2012, 2014 等 declare @agent t ...
- android 连接USB按power键锁屏2声锁屏音
alps\frameworks\base\packages\Keyguard\src\com\android\keyguard\KeyguardViewMediator.java #1384 行左右: ...
- 【codeforces 779A】Pupils Redistribution
[题目链接]:http://codeforces.com/contest/779/problem/A [题意] 让你把两个组的5个人的数目都变成一样的. 支持交换操作; 问你最少需要交换几次. [题解 ...