配置文件的读取-程序本身配置文件或自定义配置文件

程序本身配置文件(即app.config)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <connectionStrings>
    <add name="SqlServerConnstring" connectionString="server=localhost;user id=sa;password=123123;database=Hnt.MES.ZHBS2.Base"/>
  </connectionStrings>
  <appSettings>
    <!--测试配置数据-->
    <add key="TestUserXmlconfig" value="\\Configs\\SystemConfig\\TestUser.xml$/TestUserList/TestUser"/>
  </appSettings>
</configuration>
  • 数据库配置节点的使用(即connectionStrings节点):
 /// <summary>
 /// sqlserver数据库连接
 /// </summary>
public static string SqlServerConnstring = System.Configuration.ConfigurationManager.ConnectionStrings["SqlServerConnstring"].ConnectionString;
  • 自定义配置节点数据使用:
/// <summary>
/// TestUserXmlconfig  是配置文件中key值
/// </summary>
public static string TestUserXmlconfig = ConfigurationManager.AppSettings["TestUserXmlconfig"].ToString();

自定义文件保存配置信息(可以是config文件也可以是xml文件等

  • 自定义config文件保存配置信息
    如下图自定义TestUser.config文件保存员工配置信息:
    在这里插入图片描述读取该配置文件的数据信息如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--属性及信息-->
    <add key="Id" value="1"/>
    <add key="LoginName" value="测试数据"/>
    <add key="Name" value="测试数据"/>
    <add key="UserSex" value="测试数据"/>
    <add key="UserPost" value="测试工位"/>
    <add key="Password" value="测试数据"/>
    <add key="UserState" value="测试数据"/>
    <add key="Del_Sign" value="T"/>
    <add key="AddUser" value="测试数据"/>
    <add key="AddTime" value="2017-07-15"/>
    <add key="UpdateUser" value="测试数据"/>
    <add key="UpdateTime" value="2017-07-15"/>
    <add key="DepartmentCode" value="CESHI"/>
    <add key="DepartmentName" value="测试数据"/>
    <add key="Team" value="测试组"/>
  </appSettings>
</configuration>

读取配置该配置文件的数据方法如下:

  /// <summary>
  /// 初始化配置信息数据
  /// </summary>
  internal static void InitConfig()
  {
      Configuration testUserConfig;
      ExeConfigurationFileMap testUserConfigMap = new ExeConfigurationFileMap();
      testUserConfigMap.ExeConfigFilename = System.Environment.CurrentDirectory + "\\Configs\\UserConfig\\TestUser.config";
      testUserConfig = ConfigurationManager.OpenMappedExeConfiguration(testUserConfigMap, ConfigurationUserLevel.None);
      //
      TestUserClass t = new TestUserClass();
      t.Id =Convert.ToInt64(testUserConfig.AppSettings.Settings["Id"].Value);
      t.LoginName = testUserConfig.AppSettings.Settings["LoginName"].Value;
      t.Name = testUserConfig.AppSettings.Settings["Name"].Value;
      t.UserSex = testUserConfig.AppSettings.Settings["UserSex"].Value;
      t.UserPost = testUserConfig.AppSettings.Settings["UserPost"].Value;
      t.Password = testUserConfig.AppSettings.Settings["Password"].Value;
      t.UserState = testUserConfig.AppSettings.Settings["UserState"].Value;
      t.Del_Sign = testUserConfig.AppSettings.Settings["Del_Sign"].Value;
      t.AddUser = testUserConfig.AppSettings.Settings["AddUser"].Value;
      t.AddTime=Convert.ToDateTime(testUserConfig.AppSettings.Settings["AddTime"].Value);
      t.UpdateUser = testUserConfig.AppSettings.Settings["UpdateUser"].Value;
      t.UpdateTime = Convert.ToDateTime(testUserConfig.AppSettings.Settings["UpdateTime"].Value);
      t.DepartmentCode = testUserConfig.AppSettings.Settings["DepartmentCode"].Value;
      t.DepartmentName = testUserConfig.AppSettings.Settings["DepartmentName"].Value;
      t.Team = testUserConfig.AppSettings.Settings["Team"].Value;
      testUserList.Add(t);
  }
  • 自定义xml文件保存配置信息
    如下图定义Hnt_User.xml保存用户配置数据:
    在这里插入图片描述该配置文件的数据如下:
<?xml version="1.0" encoding="utf-8" ?>
<Hnt_UserList>
  <!--注释-->
  <Hnt_User>
    <Id>2</Id>
    <LoginName>admin</LoginName>
    <UserName>超级管理员</UserName>
    <UserSex>管理员</UserSex>
    <UserPost>管理员</UserPost>
    <Password>202cb962ac59075b964b07152d234b70</Password>
    <UserState>超级管理员</UserState>
    <Del_Sign>S</Del_Sign>
    <AddUser>system</AddUser>
    <AddTime>2019-01-10 11:38:33.560</AddTime>
    <UpdateUser>NULL</UpdateUser>
    <UpdateTime>NULL</UpdateTime>
    <DepartmentCode>NULL</DepartmentCode>
    <DepartmentName>NULL</DepartmentName>
    <UserPhoto>NULL</UserPhoto>
    <UserQualification>NULL</UserQualification>
    <Team>NULL</Team>
    <CardCode>NULL</CardCode>
    <WeChatId>NULL</WeChatId>
    <OpenId>NULL</OpenId>
    <Token>NULL</Token>
  </Hnt_User>
</Hnt_UserList>

读取该配置文件数据的方法如下:

   /// <summary>
   /// xml节点数据转换为对象集合
   /// </summary>
   /// <typeparam name="T">泛型接受对象</typeparam>
   /// <param name="file">xml文件路径</param>
   /// <param name="Node">xml节点对象(即Hnt_User)</param>
   /// <returns></returns>
   public static List<T> LoadUserXml<T>(string file, string Node)
   {
       List<T> list_t = new List<T>();         
       XmlDocument doc = new XmlDocument();
       doc.Load(file);
       XmlNodeList list = doc.SelectNodes(Node);
       foreach (XmlNode item in list)
       {
           list_t.Add(XmlHelper.LoadConfiguration<T>(item));
       }
       return list_t;
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值