zoukankan      html  css  js  c++  java
  • C#读取App.config/Web.config

    读取需要添加 System.Configuration 引用,

    两种方式添加:

    1:.NETFramework程序可以在引用右击添加引用,然后添加System.Configuration 

    2:引入Nuget 包:Install-Package   System.Configuration.ConfigurationManager 

    实例如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <appSettings>
        <add key="a" value="A"/>
        <add key="b" value="B"/>
        <add key="c" value="C"/>
      </appSettings>
      <connectionStrings>
        <add name="MSSQL" connectionString="Server..." providerName="111"/>
        <add name="MYSQL" connectionString="Server....." providerName="222"/>
      </connectionStrings>
    </configuration>
    App.config
    using System;
    using System.Configuration;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(ConfigurationManager.AppSettings["a"]);
                Console.WriteLine(ConfigurationManager.AppSettings["b"]);
                Console.WriteLine(ConfigurationManager.AppSettings["c"]);
                Console.WriteLine();
                Console.WriteLine(ConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString);
                Console.WriteLine(ConfigurationManager.ConnectionStrings["MSSQL"].ProviderName);
                Console.WriteLine();
                Console.WriteLine(ConfigurationManager.ConnectionStrings["MYSQL"].ConnectionString);
                Console.WriteLine(ConfigurationManager.ConnectionStrings["MYSQL"].ProviderName);
            }
        }
    }
    读取App.config
    //Asp.Net应用程序(MVC)读取Web.config
    string name = System.Web.Configuration.WebConfigurationManager.AppSettings["name"];
    读取Web.config
  • 相关阅读:
    Sexy Beach PR 汉化补丁+入门教程
    [Unity3D]Script 脚本全部编译器属性具体解释
    图论--最小生成树和最短路1
    软件设计师之路总结~引——时间的温度
    BZOJ1441: Min
    BZOJ1602: [Usaco2008 Oct]牧场行走
    BZOJ1600: [Usaco2008 Oct]建造栅栏
    BZOJ1599: [Usaco2008 Oct]笨重的石子
    BZOJ1601: [Usaco2008 Oct]灌水
    BZOJ1058: [ZJOI2007]报表统计
  • 原文地址:https://www.cnblogs.com/RainFate/p/12082497.html
Copyright © 2011-2022 走看看