首页 > 学院 > 开发设计 > 正文

文章标题

2019-11-08 03:24:01
字体:
来源:转载
供稿:网友

获取配置文件中的值

在配置文件中添加数据

<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <customErrors mode="Off" /> </system.web> <appSettings> <add key="name1" value="AAAAAA" /> <add key="name2" value="111111" /> </appSettings> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>

获取数据方法

定义一个ConfigHelper类来获取这两个数据。using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; namespace DDSDK { public class ConfigHelper { #region FetchName1 /// <summary> /// 获取name1 /// </summary> /// <returns></returns> public static String Fetchname1 () { return FetchValue("name1 "); } #endregion #region Fetchname2 Function /// <summary> /// 获取name2 /// </summary> /// <returns></returns> public static String Fetchname2() { return FetchValue("name2"); } #endregion #region FetchValue Function PRivate static String FetchValue(String key) { String value = ConfigurationManager.AppSettings[key]; if (value == null) { throw new Exception($"{key} is null.请确认配置文件中是否已配置."); } return value; } #endregion } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表