C# winform配置文件读取和ConfigrationManager.OpenExeConfiguration(exePath)的误导性错误

在修改App.config配置文件时

例如:

///<summary> 
///更新连接字符串 
///</summary> 
///<param name="newName">连接字符串名称</param> 
///<param name="newConString">连接字符串内容</param> 
///<param name="newProviderName">数据提供程序名称</param> 
private static void UpdateConnectionString(string newName, string newConString, string newProviderName)
{
    // 将当前应用程序的配置文件作为 System.Configuration.Configuration 对象打开
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 
    // 存在此连接,则先删除
    if (ConfigurationManager.ConnectionStrings[newName] != null)
    {
        config.ConnectionStrings.ConnectionStrings.Remove(newName);
    }
 
    // 新建一个连接字符串
    ConnectionStringSettings newConStr = new ConnectionStringSettings(newName, newConString, newProviderName);
 
    // 将新的连接字符串添加到配置文件中. 
    config.ConnectionStrings.ConnectionStrings.Add(newConStr);
 
    // 保存对配置文件所作的更改 
    config.Save(ConfigurationSaveMode.Modified);
 
    // 强制重新载入配置文件的ConnectionStrings配置节 
    ConfigurationManager.RefreshSection("ConnectionStrings");
}

在 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);中经常出现

读取和ConfigrationManager.OpenExeConfiguration(exePath)的误导性错误

----------------------------------------------------------------------------------------------
首先说ConfigrationManager.OpenExeConfiguration(exePath)的问题:
1.此处是一个bug,exePath在msdn的说明中是要读取的配置文件的路径,而在生成System.Configuration.Configuration a = System.Configuration.ConfigurationManager.OpenExeConfiguration(exepath)的时候,实际在生成的a对象中,可以看到它的FilePath属性,让人大吃一惊,它显示的路径是原exePath路径+.config.
所以a对象中永远为空,因为你的X.config是存在的,但它读取的却是X.config.config.

这是否是我们的理解有误而非Bug呢?
不,在试验在exePath中填写.config前面的部分后,提示错误,无法读取该配置文件,即它的ConfigurationManager.OpenExeConfiguration()方法要求exepath必须是一个.config文件!!!

2.这个Bug可能的原因
我猜想,它原来的读取的方法和类都是针对的配置文件的形式为:可执行文件文件名+.exe+.config的形式,或者是Machine.config.而这时不需要识别.config的,而当在.net 2.0里面时候,添加的这个读取额外配置文件的方法,由于一些失误,仍然是匹配的原来的老方法,导致它在读取是自动加上了.config.
---------------------------------------------------------------------------------------------
解决方法
我参考了<Configuration类在网页实现对web.config的修改>(http://tech.sina.com.cn/s/2008-06-30/1013712947.shtml)的方法
即真假config文件的方法:
1.先在项目的bin/debug目录下建立一个额外的config文件,即X.config;
2.在同目录下建立一个X.config.config文件;
3.所有信息都在X.config.config内,即appSettings节点等;
4.然后读写都可以了.
-----------------------------------------------------------------------------------------------
我写的一个小例子: 
1.读取配置文件信息的代码

复制代码
 //配置文件的路径,此处为配置文件(假)的路径
            string path=Application.StartupPath+"\\App2.1.config";
            
//显示结果,即配置文件的MachineID字段的值
            string showContent = "";

            
//a对象从额外的配置文件(真)生产,而exepath,即path填写的是配置文件(假)的路径,而配置文件(真)的路径=配置文件(假)的路径+.config;而配置文件(假)的文件名中,最后也应是.config结尾.
            System.Configuration.Configuration a = System.Configuration.ConfigurationManager.OpenExeConfiguration(path);

            
//如果配置文件中不包括appSettings节,则抛出异常.
            if (a.Sections["appSettings"== null)
            
{
                
throw new Exception("该配置文件中不存在appSettings节点");
            }

            
else
            
{
                showContent 
= a.AppSettings.Settings["MachineID"].Value.ToString();
                
            }
         
            
//Label显示配置文件中的MachineID的值
            label1.Text = showContent;
复制代码


2.配置文件名称:App2.1.config.config
内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<appSettings>     
    
<add key="MachineID" value="App2.1 Long"/>   
  
</appSettings>

</configuration>



3.最后读取出的结果显示在label中,为:App2.1 Long

posted on 2013-06-09 11:13  luoxq39  阅读(1391)  评论(0编辑  收藏  举报