读取app.config配置的相关问题
2018-01-30 17:29
375 查看
最 4000 近用到需要在App.config 文件中配置相关内容的代码,遇到过一些问题,总结了一下: 首先如果配置文件中有 configSections 节点,该节点必须放在第一位否则会出现各种问题。 自定义的配置必须在 configSections 节点中进行声明,否则读取相关Section信息的时候返回值为null。 如下给出一段在 App.config 文件中自定义配置的代码:
App.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="loginUrl" value="~/RedirectValue.cshtml" /> <add key="autoFormsAuthentication" value="false"/> </appSettings> <configSections> <!-- Servcies Defination --> <section name="services" type="ServciesHost.ServicesConfig, ServciesHost"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/> </startup> <services> <confs> <service id="0001" name="TestService1" path="TestService1.exe" description="TestService1"/> <service id="0002" name="TestService2" path="TestService2.exe" description="TestService2"/> </confs> </services> </configuration>
ServiceElement.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; namespace ServciesHost { public class ServiceElement:ConfigurationElement { [ConfigurationProperty("id")] public string Id { get { return (string)this["id"]; } set { this["id"] = value; } } [ConfigurationProperty("name")] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("path")] public string Path { get { return (string)this["path"]; } set { this["path"] = value; } } [ConfigurationProperty("description")] public string Description { get { return (string)this["description"]; } set { this["description"] = value; } } } }
ServcieCollection.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; namespace ServciesHost { public class ServcieCollection:ConfigurationElementCollection { public ServiceElement this[int index] { get { return (ServiceElement)BaseGet(index); } } public new ServiceElement this[string id] { get { return (ServiceElement)BaseGet(id); } } protected override ConfigurationElement CreateNewElement() { return new ServiceElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((ServiceElement)element).Id; } } }
ServicesConfig.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.IO; using System.Reflection; namespace ServciesHost { public class ServicesConfig:ConfigurationSection { private static ServicesConfig _servicesConfig = null; [ConfigurationProperty("confs", IsDefaultCollection = true)] [ConfigurationCollection(typeof(ServcieCollection), AddItemName = "service")] public ServcieCollection Services { get { return (ServcieCollection)this["confs"]; } } public static ServicesConfig CurrentConfig { get { return _servicesConfig; } } private static Configuration _config; public static ServicesConfig Initialize() { try { _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); _servicesConfig = _config.GetSection("services") as ServicesConfig; } catch (Exception ex) { throw ex; } return _servicesConfig; } } public class NameValueSection : ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true)] public NameValueConfigurationCollection Settings { get { return (NameValueConfigurationCollection)base[""]; } } } }
Program.cs
using CommonLibary; using System.Configuration; using System; namespace ServciesHost { class Program { static void Main(string[] args) { ServicesConfig.Initialize(); Console.ReadLine(); } } }
相关文章推荐
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
- 类项目中的配置文件app.config在打包安装后的信息获取的问题
- C#_App.config配置文件的读取与更新
- vb.net 无法读取app.config中的配置参数的解决方式
- MVC.Net:读取Web.config/App.config配置
- winform程序读取和改写配置文件App.config元素的值
- C#读取app.config数据库配置字符串
- winform中读取配置文件App.config的一些事
- winform程序读取和改写配置文件App.config元素的值
- 关于 App.config文件出错,配置系统未能初始化。 问题解决方案
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- winform读取配置文件App.config
- c++/cli 读取配置文件app.config所需的设置
- 关于程序集配置文件(App.config)的问题
- JAVA相关问题:java读取properties配置文件路径问题
- winform程序读取和改写配置文件App.config元素的值
- Window form中App.config配置文件的添加和读取
- C#基本知识 -- App.config文件的配置与读取
- C# 读取保存App.config配置文件的完整源码参考
- c#读取配置文件 程序的App.config