dotnet 从 app.config 中获取配置信息示例

发布时间:2023年12月20日

dotnet 代码

// 配置信息类
public static class Config
    {
        // 获取 Configuration 对象
       static Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        public static string Token
        {
            get
            {
                // 根据 Key 读取 <add> 元素的 Value
                return _config.AppSettings.Settings["Token"].Value;

            }
        }

        public static string connectionString
        {
            get
            {
                return _config.AppSettings.Settings["connectionString"].Value;

            }
        }
        public static string filePath
        {
            get
            {
                return _config.AppSettings.Settings["filePath"].Value;

            }
        }
    }

app.config 中节点配置

// app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="connectionString" value="Server=192.168.6.226;Port=5432;Database=odp20210825;User Id=user;Password=pwd;"/>
    <add key="filePath" value="C:\Users\lealzhang-pc\Desktop\说明书\simple\"/>
    <add key="Token" value="Basic basiceyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYXBwaWQiOiJYY3JtIn0..RSkhFbVtVAdCHWQMtXk5dg.752-ef7D5nPVcnA4msUUDKiWDswTS3M_pGKLk_D9p2cuu4U_bUlsdsPS86etbaxSZQ4ZDJkKDOsCsNWRTAvxzlzdX99eu5ZS8fKV_KghlTUPpzTMoJyuYJkJP7S2zLrtwSvmT3wiAb6LKqNNvwgp50xbu_zIgAjvex25gVsliCETGAUeLVyW6ec1TpY41i3kgTEwLugx8pAeM2YFyJsATM-SOlewqvzaK5boGyN7wW0.OCM7awoqPdbxXkPnzgzwvA"/>
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

文章来源:https://blog.csdn.net/qq_41609957/article/details/135101544
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。