C#读取App.config/Web.config (.NETFramework版本)

读取需要添加 System.Configuration 引用,实例如下:

 

C#读取App.config/Web.config  (.NETFramework版本)
<?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

 

C#读取App.config/Web.config  (.NETFramework版本)
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);
        }
    }
}
读取

C#读取App.config/Web.config (.NETFramework版本)

上一篇:2021-02-06


下一篇:uni-app引入自定义图标库