zoukankan      html  css  js  c++  java
  • VS2005中读写配置文件(方法一)(转载)

    VS2003中对于应用程序配置文件(app.config或者web.config)只提供了读取的功能。而在VS2005中,对于配置文件的功能有了很大的加强。
    在VS2005中,对于应用程序配置文件的读写一般使用Configuration,ConfigurationManager两个类。
    ConfigurationManager类为客户应用程序提供了一个访问的功能。
    使用ConfigurationManager对象执行打开配置文件的操作后,将会返回一个Configuration的对象。
    通过程序实现读写配置文件的代码如下所示:
    1     创建配置文件中的配置节所对应的类。该类必须继承自ConfigurationSection

    public sealed class ConfigurationSections : ConfigurationSection
           
    {
               [ConfigurationProperty(
    "filename", DefaultValue = "default.txt")]
               
    public string FileName
               
    {
                   
    get
                   
    {
                       
    return (string)this["filename"];
                   }

                   
    set
                   
    {
                       
    this["filename"= value;
                   }

               }

           }

           
    public sealed class BusinessSpaceConfiguration : ConfigurationSection
           
    {
               [ConfigurationProperty(
    "filename")]
               
    public string FileName
               
    {
                   
    get
                   
    {
                       
    return (string)this["filename"];
                   }

                   
    set
                   
    {
                       
    this["filename"= value;
                   }

               }

           }

    2     创建配置文件代码

    private static void WriteAppConfiguration()
               
    {
                   
    try
                   
    {
                       ConfigurationSections configData 
    = new ConfigurationSections();
                       configData.FileName 
    = "abc.txt";
                       System.Configuration.Configuration     config 
    = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                       config.Sections.Remove(
    "ConfigurationSections");
                       config.Sections.Add(
    "ConfigurationSections", configData);
                       config.Save();

                       BusinessSpaceConfiguration bsconfigData 
    = new BusinessSpaceConfiguration();
                       bsconfigData.FileName 
    = "def.txt";
                       System.Configuration.Configuration config1 
    = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                       config1.Sections.Remove(
    "BusinessSpaceConfiguration");
                       config1.Sections.Add(
    "BusinessSpaceConfiguration", bsconfigData);
                       config1.Save();                      
                   }

                   
    catch (Exception err)
                   
    {
                       Console.Write(err.Message);
                   }

               }


    3     生成的配置文件格式如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
           
    <configSections>
               
    <section name="BusinessSpaceConfiguration" type="ConsoleApplication1.BusinessSpaceConfiguration, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
               
    <section name="ConfigurationSections" type="ConsoleApplication1.ConfigurationSections, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
           
    </configSections>
           
    <BusinessSpaceConfiguration filename="def.txt" />
           
    <ConfigurationSections filename="abc.txt" />
    </configuration>


    3     读取应用程序配置文件

    private static void ReadAppConfiguration()
               
    {
                   ConfigurationSections obj1 
    = ConfigurationManager.GetSection("ConfigurationSections"as ConfigurationSections;
                   BusinessSpaceConfiguration obj2 
    = ConfigurationManager.GetSection("BusinessSpaceConfiguration"as BusinessSpaceConfiguration;
                   Console.WriteLine(obj1.FileName);
                   Console.WriteLine(obj2.FileName);

               }

    本文转自
    http://hi.baidu.com/geminiboy2008/blog/item/e59dee2a1f441899023bf647.html

  • 相关阅读:
    自定义IP原来如此简单
    [转]如何在NIOS II中读写EPCS剩余空间
    坏了的芯片居然又好了一片,太神奇了
    今天报废两片EP3C5E144
    Quartus II 订购版 v10.1 正式推出下载
    发现用JTAG下载程序到EPCS比用AS方式下载速度快
    如何解决No EPCS layout data looking for section [EPCSXXXXXX]
    QII丰衣足食
    Why does my Cyclone III FPGA fail to access the EPCS device using the EPCS Controller module?
    <转载>在.NET中基于Windows消息的IPC实现
  • 原文地址:https://www.cnblogs.com/mlog/p/2456411.html
Copyright © 2011-2022 走看看