惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
S
Secure Thoughts
月光博客
月光博客
美团技术团队
雷峰网
雷峰网
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
News and Events Feed by Topic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Forbes - Security
Forbes - Security
W
WeLiveSecurity
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
G
GRAHAM CLULEY
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
Last Week in AI
Last Week in AI
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
云风的 BLOG
云风的 BLOG
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Announcements
Recent Announcements
Webroot Blog
Webroot Blog
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
罗磊的独立博客
The Register - Security
The Register - Security
Blog — PlanetScale
Blog — PlanetScale
T
Threat Research - Cisco Blogs
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
B
Blog
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news
IT之家
IT之家
D
DataBreaches.Net
博客园 - 司徒正美
N
Netflix TechBlog - Medium
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 经霄

Microsoft ProClarity 6.3 releases to manufacturing Windows Services中使用MSBuild实现Build Automation Management VSS Object Model Google Service 使用反射动态创建类的实例 - 经霄 - 博客园 AS2005中的维度安全性简介【转】 学位英语通过,特此庆祝一下 VS2005常用插件 AMO编程 - 经霄 - 博客园 Analysis Management Object对象模型 华为前员工:揭密华为“薪酬真相”【转】 微软笔试试题——11月13日,2005年 Structure of the Basic MDX Query(转) 使用ADOMD.NET建立与Analysis Services的连接 IT公司薪水调查 ADOMD.net概述 SQL与MDX语法的比较 AdventureWorks数据库的安装 多维联机数据分析模型和系统设计方法[转]
VS2005中读写配置文件(一)
经霄 · 2005-11-28 · via 博客园 - 经霄

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);

        }