VS2005配置文件(.config/Settings)

一、.config设置

      NET的应用程序配置文件,使用的是XML格式。相对INI文件来说,它的功能要强上不少,而且具有很强的可扩展性。它的缺点是不能直接进行写操作,也就是说,不能直接在程序中修改配置文件的数据(当然不是指不能,不过不是本文讨论的范围)。本文主要目的是探讨如何扩展配置文件,并在其加入各种自定义配置信息。
 

 1. 使用<appSettings>
        简单的配置信息,可以直接放入<appSettings>标记中。如:

<?xml version="1.0" encoding="utf-8"?>
  
<appSettings>
 
<add key="LogFile" value="d:\log\debug.log"/>
  
</appSettings>  
</configuration>

        相应访问代码如下:       

string fileName = System.Configuration.ConfigurationSettings.AppSettings.Get("LogFile");

     2. 自定义配置节(section)名称
        比如,我们要使用下面的配置结构,将配置信息归类分组:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- 需要在此处加入自定义配置声明 -->
<!-- 以下是自定义配置的内容 -->
<myConfig>
  
<myDictionary>
    
<add key="Area" value="Fuzhou"/>
    
<add key="Device" value="Printer"/> 
    
<add key="Customer" value="Muf"/>

  
</myDictionary>
  
<myNameValue>
    
<add key="Area" value="Fuzhou"/>
    
<add key="Device" value="Printer"/> 
    
<add key="Customer" value="Muf"/>

  
</myNameValue>
  
<myInfo
    
Area="Fuzhou" Device="Printer" Customer="Muf"

  
/>
</myConfig>
</configuration>

        但是光这样子说明是不行的。没有声明,是不能使用自定义的配置段。我们必须要在配置文件前面加入声明:  

<!-- 以下是自定义配置的声明 -->
  
<configSections>
    
<sectionGroup name="myConfig">
         
<section name="myDictionary"
            type
="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        
<section name="myNameValue"
            type
="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        
<section name="myInfo"
            type
="System.Configuration.SingleTagSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    
</sectionGroup>
  
</configSections>  

    声明和配置的关系,示意图如下:
        
    由图上可以看出,NameValueSectionHandler和DictionarySectionHandler在定义配置文件的内容形式上是一样的,都是用<add>来设置内容的。只是返回到C#中的类不太一样,可以参考下面的代码示例。
    另外,如果不关心Handler类的版本等信息,可以直接省略。如NameValueSectionHandler可以直接如下声明:

<section name="myDictionary"            type="System.Configuration.NameValueSectionHandler, System" />

    把上面的<configSections>声明段放入配置文件中,我们的配置结构就可以正常使用了。声明中,< sectionGroup>用来定义不含配置数据的节的名称。<section>用来定义含有自定义配置数据的节的名称。< section type>用来指定定义配置数据的类型。

    注意,自定义的配置节,不能使用 System.Configuration.ConfigurationSettings.AppSettings.Get 来访问,要使用 System.Configuration.ConfigurationSettings.
GetConfig

    .NET已经定义了3种配置类型:
  a. NameValueSectionHandler
        相应访问代码如下: 

NameValueCollection myNameValue= (NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig/myNameValue");
string Area = myNameValue["Area"
];
string Device= myNameValue["Device"
];
string Customer = myNameValue["Customer "
];

  b. DictionarySectionHandler
        相应访问代码如下:

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig/myDictionary");
string Area = myNameValue["Area"
];
string Device= myNameValue["Device"
];
string Customer = myNameValue["Customer "
];

  c. SingleTagSectionHandler
        相应访问代码如下:   

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig/myInfo");
string Area = myNameValue["Area"
];
string Device= myNameValue["Device"
];
string Customer = myNameValue["Customer "
];

        这三种类型的详细信息,可以参考 MSDN 文档。同时.NET 还定义了IgnoreSectionHandler类型,为 System.Configuration 之外的系统所读取和处理的配置节提供节处理程序定义。
        除此之外,.NET提供了IConfigurationSectionHandler接口,这样我们还可以自行进行扩展,以设计出我们自已的配置形式。

 

3. 自定义配置结构 (使用IConfigurationSectionHandler)
 假设有以下的配置信息,其在MyInfo可以重复许多次,那么应如何读取配置呢?这时就要使用自定义的配置程序了。

<myConfigs>
  
<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
  
<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
</myConfig>

 访问代码如下:

Hashtable cfgTable = (Hashtable)ConfigurationSettings.GetConfig( "myConfigs" );

Debug.Assert( cfgTable.Count 
== 2
);
Hashtable cfgFuzhou 
= (Hashtable)cfgTable["Fuzhou"
];
Hashtable cfgShanghai 
= (Hashtable)cfgTable["Shanghai"
];
Debug.Assert( cfgFuzhou[
"Device"== "Printer"
 );
Debug.Assert( cfgShanghai[
"Device"== "Mobile"
 );
Debug.Assert( cfgFuzhou[
"Customer"== "Muf"
 );
Debug.Assert( cfgShanghai[
"Customer"== "Liny"
 );

foreach(Hashtable cfg in
 cfgTable.Values)
{
 Console.WriteLine(
"Area={0} Device={1} Customer={2}", cfg["Area"], cfg["Device"], cfg["Customer"
]);
}


 为了能使用上面的访问代码来访问配置结构,我们需要生成一个特定的配置读取类(ConfigurationSectionHandler),例子很简单,就不多做说明了:

public class MyInfoSectionHandler: IConfigurationSectionHandler
{
 
public object Create(object parent, object
 configContext, System.Xml.XmlNode section)
 
{
  Hashtable config 
= new
 Hashtable();
  
foreach(XmlNode node in
 section.ChildNodes)
  
{
   
if(node.Name != "myInfo"
)
    
throw new System.Configuration.ConfigurationException("不可识别的配置项"
, node);

   Hashtable item 
= new
 Hashtable();
   
foreach(XmlAttribute attr in
 node.Attributes)
   
{
    
switch
(attr.Name)
    
{
     
case "Area"
:
     
case "Device"
:
     
case "Customer"
:
      item.Add(attr.Name, attr.Value);
      
break
;
     
default
:
      
throw new System.Configuration.ConfigurationException("不可识别的配置属性"
, attr);
    }

   }

   config.Add(item[
"Area"], item);
  }

  
return config;
 }

}


 然后,我们再定义配置说明。其中,myNamespace.MyInfoSectionHandler 是MyInfoSectionHandler类的带名字空间的完整名称;myApp 则是定义MyInfoSectionHandler类的程序集不带扩展名的名字(如myApp.dll或myApp.exe):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  
<!-- 以下是自定义配置的声明 -->
  
<configSections>
      
<section name="myConfig" type="myNamespace.MyInfoSectionHandler, myApp" />
  
</configSections>   
  
<myConfigs>

    
<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
    
<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
  
</myConfig>
</configuration>

 根据上面的例子,我们可以使用IConfigurationSectionHandler来实现任意的配置文件结构。

 

参考:1.如何使用.NET配置文件(一)

        2. 如何使用.NET配置文件(二)  

 

二、Settings.settings 设置

      可以使用数据网格视图,很方便
     
 在VS 2005中设置和读取配置文件已经变的很简单了,而且是强类型的,读取的值可以直接赋值给相应的变量,无需强制转换。
      例如:DateTime userDateTime1 = Properties.Settings.Default.userDateTime1; 真是方便了很多。但是你有没有发现,使用 Properties.Settings.Default.Save() 保存了设置后,Application 范围的设置为什么没有保存成功,User 范围的设置的变化为什么没有体现到 app.config 文件中去呢?

1. 在VS 2005中进行应用程序设置
    打开 项目属性 » 设置,如下图:
   
   输入名称,选择类型和范围,输入值保存即完成设置。
   类型:int,string,DateTime等各种数据类型;
   范围:Application  范围的设置对所有用户都有效;
           User 范围的设置对当前用户(当前 Windows 登录的用户)有效,同一个设置每个用户可以有不同的值,而且互不影响。
(把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改。)


2. 读取配置文件(读取应用程序设置)
     无论是Application  范围的设置,还是User 范围的设置,读取的方法都是一样的。

    // 读取设置
    this.appSetting1TextBox.Text = Properties.Settings.Default.appSetting1;
    
this.userSetting1TextBox.Text = Properties.Settings.Default.userSetting1;


3. 保存 User 范围配置文件(保存 User 范围的应用程序设置)   

    // 保存 User 范围的设置
    Properties.Settings.Default.userSetting1 = this.userSetting1TextBox.Text;
    Properties.Settings.Default.Save();

    User 范围配置文件没有保存在应用程序文件夹下,而是保存在这里:X:\Documents and Settings\Windows登录用户\Local Settings\Application Data。

4. 保存 Application 范围配置文件(保存 Application 范围的应用程序设置)     
    保存 Application 范围配置文件可没有保存 User 范围配置文件那样简单,直接 Properties.Settings.Default.Save() 是不行的。因为 Application 范围的设置在运行时是“只读”的。这里使用的方法是使用 XmlDocument 来直接保存 config 文件,然后在 Reload 设置。

    // 保存 Applicationi 范围的设置
    string configFileName = Application.ExecutablePath + ".config";
    System.Xml.XmlDocument doc 
= new
 System.Xml.XmlDocument();
    doc.Load(configFileName);
    
string configString = @"configuration/applicationSettings/SetConfig.Properties.Settings/setting[@name='appSetting1']/value"
;
    System.Xml.XmlNode configNode 
=
 doc.SelectSingleNode(configString);
    
if (configNode != null
)
    {
        configNode.InnerText 
= this
.appSetting1TextBox.Text;
        doc.Save(configFileName);
        
// 刷新应用程序设置,这样下次读取时才能读到最新的值。

        Properties.Settings.Default.Reload();
    }


顺便说一下:使用 Properties.Settings.Default.Reset() 可以恢复 User 范围设置的默认值(从 app.config 中恢复)。

源代码下载:
SetConfig.rar

  

原文地址:https://www.cnblogs.com/greatandforever/p/1371743.html