zoukankan      html  css  js  c++  java
  • ConfigurationManager 类的使用

    一、引用

    命名空间:   System.Configuration
    程序集:  System.Configuration(位于 System.Configuration.dll)

    二、示例

    1、读取、增加、修改配置

    using System;
    using System.Configuration;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ReadAllSettings();
                ReadSetting("Setting1");
                ReadSetting("NotValid");
                AddUpdateAppSettings("NewSetting", "May 7, 2014");
                AddUpdateAppSettings("Setting1", "May 8, 2014");
                ReadAllSettings();
            }
    
            static void ReadAllSettings()
            {
                try
                {
                    var appSettings = ConfigurationManager.AppSettings;
    
                    if (appSettings.Count == 0)
                    {
                        Console.WriteLine("AppSettings is empty.");
                    }
                    else
                    {
                        foreach (var key in appSettings.AllKeys)
                        {
                            Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
                        }
                    }
                }
                catch (ConfigurationErrorsException)
                {
                    Console.WriteLine("Error reading app settings");
                }
            }
    
            static void ReadSetting(string key)
            {
                try
                {
                    var appSettings = ConfigurationManager.AppSettings;
                    string result = appSettings[key] ?? "Not Found";
                    Console.WriteLine(result);
                }
                catch (ConfigurationErrorsException)
                {
                    Console.WriteLine("Error reading app settings");
                }
            }
    
            static void AddUpdateAppSettings(string key, string value)
            {
                try
                {
                    var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    var settings = configFile.AppSettings.Settings;
                    if (settings[key] == null)
                    {
                        settings.Add(key, value);
                    }
                    else
                    {
                        settings[key].Value = value;
                    }
                    configFile.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
                }
                catch (ConfigurationErrorsException)
                {
                    Console.WriteLine("Error writing app settings");
                }
            }
        }
    }

    在文件 App.config中:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
      <appSettings>
        <add key="Setting1" value="May 5, 2014"/>
        <add key="Setting2" value="May 6, 2014"/>
      </appSettings>
    </configuration>

    2、用配置文件里的连接字符串读取数据库里的数据:

    using System;
    using System.Configuration;
    using System.Data.SqlClient;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ReadProducts();
            }
    
            static void ReadProducts()
            {
                var connectionString = ConfigurationManager.ConnectionStrings["WingtipToys"].ConnectionString;
                string queryString = "SELECT Id, ProductName FROM dbo.Products;";
                using (var connection = new SqlConnection(connectionString))
                {
                    var command = new SqlCommand(queryString, connection);
                    connection.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
                        }
                    }
                }
            }
        }
    }

     an App.config文件中:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
        <connectionStrings>
          <add name="WingtipToys" connectionString="Data Source=(LocalDB)v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False" />
        </connectionStrings>
    </configuration>
  • 相关阅读:
    可图性判定HavelHakimi定理
    并查集入门
    js数组和函数应用
    DOM用法及应用
    javascript基础知识
    表单
    PHP变量
    30天自制操作系统开发笔记——IDT中断描述符表
    《30天自制操作系统》学习笔记——汇编程序磁盘BIOS调用
    汇编指令: LGDT、LIDT、LLDT、LMSW、LOADALL、LOADALL286、LOCK、LODSB、LODSW、LODSD
  • 原文地址:https://www.cnblogs.com/dayang12525/p/7866766.html
Copyright © 2011-2022 走看看