应用程序
配置文件是标准的 XML
文件,XML
标记和
属性是区分大小写的。它是可以按需要更改的,开发人员可以使用
配置文件来更改设置,而不必重编译应用程序。
配置文件的根节点是con
figuration。我们经常访问的是appSettings,它是由.Net预定义配置节。我们经常使用的
配置文件的架构是象下面的形式。
先大概有个印象,通过后面的实例会有
一个比较清楚的认识。下面的“配置节”可以理解为进行配置
一个XML的节点。
1.
向项目添加
app.config
文件:
<?
xml
version
=
"1.0 "encoding = "utf-8 " ?>
在项目进行编译后,在
bin/Debuge
文件下,将出现两个配置文件
(
以本项目为例
)
,一个名为“
JxcManagement.EXE.config
”,另一个名为“
JxcManagement.vshost.exe.config
”。第一个文件为项目实际使用的配置文件,在程序运行中所做的更改都将被保存于此;第二个文件为原代码“
app.config
”的同步文件,在程序运行中不会发生更改。
2. connectionStrings
配置节:
请注意:如果您的
sql
版本为
2005 Express
版,则默认安装时
sql
服务器实例名为
localhost/sqlExpress
,须更改以下实例中“
Data Source=localhost;
”一句为“
Data Source=localhost/sqlExpress;
”,在等于号的两边不要加上空格。
<
connectionStrings
>
<
clear
/>
<
add
name
=
"conJxcBook "
connectionString
=
"Data Source=localhost;Initial Catalog=jxcbook;User ID=sa;password=******** "
providerName
=
"System.Data.sqlClient " />
</
connectionStrings
>
3. appSettings
配置节:
appSettings
配置节为整个程序的配置,如果是对当前用户的配置,请使用
userSettings
配置节,其格式与以下配置书写要求一样。
<!--
进销存管理系统初始化需要的参数
-->
<
appSettings
>
<
clear
/>
<
add
key
=
"userName "value = "" />
<
add
key
=
"password "value = "" />
<
add
key
=
"Department "value = "" />
<
add
key
=
"returnValue "value = "" />
<
add
key
=
"pwdPattern "value = "" />
<
add
key
=
"userPattern "value = "" />
</
appSettings
>
4.1 读取connectionStrings配置节
///
<summary>
///
依据连接串名字connectionName返回数据连接字符串
///
</summary>
///
<param name="connectionName"></param>
///
<returns></returns>
private
static string GetConnectionStringsConfig(string connectionName)
{
string
connectionString =
ConfigurationManager .ConnectionStrings[connectionName].ConnectionString.ToString();
Console .WriteLine(connectionString);
return connectionString;
}
4.2 更新connectionStrings配置节
///
<summary>
///
更新连接字符串
///
</summary>
///
<param name="newName">
连接字符串名称
</param>
///
<param name="newConString">
连接字符串内容
</param>
///
<param name="newProviderName">
数据提供程序名称
</param>
private
static void UpdateConnectionStringsConfig(string newName,
string newConString,
string newProviderName)
{
bool isModified = false ; //
记录该连接串是否已经存在
//
如果要更改的连接串已经存在
if (ConfigurationManager .ConnectionStrings[newName] != null )
{
isModified = true ;
}
ConnectionStringSettings mySettings =
new ConnectionStringSettings (newName,newConString,newProviderName);
ConfigurationManager .OpenExeConfiguration(ConfigurationUserLevel .None);
if (isModified)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
config.Save(ConfigurationSaveMode .Modified);
//
强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager .RefreshSection("ConnectionStrings" );
}
4.3 读取appStrings配置节
///
<summary>
///
返回*.exe.config文件中appSettings配置节的value项
///
</summary>
///
<param name="strKey"></param>
///
<returns></returns>
private
static string GetAppConfig(string strKey)
{
foreach (string key in ConfigurationManager .AppSettings)
{
if (key == strKey)
{
return ConfigurationManager .AppSettings[strKey];
}
}
return null ;
}
4.4 更新connectionStrings配置节
///
<summary>
///
在*.exe.config文件中appSettings配置节增加一对键、值对
///
</summary>
///
<param name="newKey"></param>
///
<param name="newValue"></param>
private
static void UpdateAppConfig(string newKey, string newValue)
{
bool isModified = false ;
foreach (string key in ConfigurationManager .AppSettings)
{
if (key==newKey)
{
isModified = true ;
}
}
// Open App.Config of executable
ConfigurationManager .OpenExeConfiguration(ConfigurationUserLevel .None);
// You need to remove the old settings object before you can replace it
if (isModified)
{
config.AppSettings.Settings.Remove(newKey);
}
// Add an Application Setting.
config.AppSettings.Settings.Add(newKey,newValue);
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode .Modified);
// Force a reload of a changed section.
ConfigurationManager .RefreshSection("appSettings" );
}
C#读写app.config中的数据
读语句:
String str
=
ConfigurationManager.AppSettings[
"
DemoKey
"
];
写语句:
Con
figurationManager.OpenExeCon
figuration(Con
figurationUserLevel.None);
cfa.AppSettings.Settings[
].Value
=
Demovalue
;
cfa.Save();
配置文件内容格式:(app.config)
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
appSettings
add
key
="DemoKey"
value
="*"
/>
</
>
红笔标明的几个关键节是必须的
Sy
stem.Con
figuration.Con
figurationSettings.AppSettings[
Key
];
但是现在FrameWork2.0已经明确表示此属性已经过时。并建议改为ConfigurationManager
或WebConfigurationManager。并且AppSettings属性是只读的,并不支持修改属性值.
但是要想调用ConfigurationManager必须要先在工程里添加system.configuration.dll程序集的引用。
(在解决方案管理器中右键点击工程名称,在右键菜单中选择添加引用,.net TablePage下即可找到)
添加引用后可以用 String str = ConfigurationManager.AppSettings["Key"]来获取对应的值了。
更新配置文件:
OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings.Add(
key
,
Name
)
||
cfa.AppSettings.Settings[
browseDir
name
;
*****************************************************************************************************************