一步一步教你玩转.NET_Framework的配置文件app.config

合集下载

解决在Web.config或App.config中添加自定义配置的方法详解

解决在Web.config或App.config中添加自定义配置的方法详解

解决在Web.config或App.config中添加自定义配置的方法详解本篇文章是对在Web.config或App.config中添加自定义配置的方法进行了详细的分析介绍,需要的朋友参考下.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持。

最近看到一些项目中还在自定义xml文件做程序的配置,所以忍不住写一篇用系统自定义配置的随笔了。

如果你已经对自定义配置了如指掌,请忽略这篇文章。

言归正传,我们先来看一个最简单的自定义配置<?xml version="1.0" encoding="utf-8" ?><configuration><configSections><section name="simple"type="ConfigExample.Configuration.SimpleSection,ConfigExample"/></configSections><simple maxValue="20" minValue="1"></simple></configuration>在配置文件中使用自定义配置,需要在configSections中添加一个section元素,并制定此section元素对应的类型和名字。

然后再在configuration根节点下面添加此自定义配置,如上例中的simple节点。

simple节点只有两个整形数的属性maxValue和minValue。

要在程序中使用自定义配置我们还需要实现存取这个配置块的类型,一般需要做如下三件事:1. 定义类型从System.Configuration.ConfigurationSection继承2. 定义配置类的属性,这些属性需要用ConfigurationProperty特性修饰,并制定属性在配置节中的名称和其他一些限制信息3. 通过基类的string索引器实现属性的get ,set非常简单和自然,如下是上面配置类的实现:public class SimpleSection:System.Configuration.ConfigurationSection{[ConfigurationProperty("maxValue",IsRequired=false,DefaultValue=Int32.MaxValue)]public int MaxValue{get{return(int)base["maxValue"];}set{base["maxValue"] = value;}}[ConfigurationProperty("minValue",IsRequired=false,DefaultValue=1)]public int MinValue{get { return (int) base["minValue"];}set { base["minValue"] = value; }}[ConfigurationProperty("enabled",IsRequired=false,DefaultValue=true)]public bool Enable{get{return (bool)base["enabled"];}set{base["enabled"] = value;}}}这样子一个简单的配置类就完成了,怎么在程序中使用这个配置呢?需要使用ConfigurationManager类(要引用System.configuration.dll这个dll只有在.Net2.0之后的版本中才有)的GetSection方法获得配置就可以了。

framework最详细的教程

framework最详细的教程

framework最详细的教程Framework是一种软件开发中的基础架构或平台,它为开发人员提供了一套工具、库和模板,以简化软件开发过程。

在本篇文章中,我将为您提供关于Framework的最详细教程,介绍它的概念、不同类型和如何使用。

一、概念和类型:1. Framework的概念:Framework是一个可重用的、可扩展的、面向对象的软件开发框架,它提供了一套通用的、可定制的解决方案,用于开发软件、应用程序、网站等。

2.框架的类型:框架可分为前端和后端两种类型。

前端框架用于开发用户界面,如HTML、CSS和JavaScript框架(如Angular、React 和Vue)。

后端框架用于服务器端开发,如Java的Spring、Python的Django和PHP的Laravel。

二、使用Framework的好处:1.提高开发效率:Framework提供了一组已经实现的功能和模块,可以节省开发人员的时间和精力。

开发人员只需关注核心业务逻辑,而无需编写重复的代码。

2.提高代码质量:由于Framework经过了严格的测试和优化,它具有良好的稳定性和高质量的代码。

使用Framework可以减少错误和漏洞的风险。

3.提供标准化解决方案:Framework提供了一组标准化的解决方案,使开发人员可以按照最佳实践和行业标准进行开发。

这有助于保持代码的一致性和可维护性。

三、使用Framework的步骤:1.选择合适的Framework:首先需要确定要开发的项目类型和需求,并选择与之匹配的Framework。

可以通过网络搜索、查看社区和论坛、阅读文档等方式了解各种不同的Framework,并选择最适合自己项目的一个。

2.学习和了解Framework的概念:在开始使用Framework之前,需要对其概念、结构和工作原理有一定的了解。

可以通过阅读Framework的官方文档、参考书籍和教程来获得相关信息。

3.安装和配置Framework:安装Framework是使用它的第一步。

.net framework用法

.net framework用法

.net framework是由微软公司开发的一个用于构建和运行应用程序的评台。

它提供了许多功能和工具,帮助开发人员快速构建稳健、可靠的应用程序,并且可以在不同的操作系统上运行。

在本文中,我们将了解.net framework的用法,包括安装、配置、开发和调试应用程序等方面的内容。

一、安装.net framework1. 确认系统要求:在安装.net framework之前,首先需要确认系统是否符合.net framework的要求。

通常情况下,Windows操作系统都是支持.net framework的,但是需要确认系统的版本是否兼容。

2. 下载.net framework安装包:可以通过微软冠方全球信息站或者其他可信赖的来源下载.net framework的安装包。

根据系统版本选择合适的安装包进行下载。

3. 安装.net framework:下载完成安装包后,双击运行安装程序,按照提示进行安装。

在安装过程中,可以选择程序集、语言包等组件进行安装,根据自己的需求选择。

二、配置.net framework1. 配置开发环境:在使用.net framework进行开发之前,需要配置开发环境。

通常情况下,可以使用Visual Studio等集成开发环境进行开发,需要安装对应的开发工具包和运行时库。

2. 配置应用程序运行环境:在部署应用程序时,需要配置应用程序运行环境。

可以使用.net framework提供的工具进行应用程序打包、发布和部署,以确保在目标系统上能够正常运行。

三、开发应用程序1. 创建项目:使用Visual Studio等开发工具,新建一个项目,选择.net framework作为目标框架。

2. 编写代码:使用C#、等编程语言编写应用程序的代码。

借助.net framework提供的类库和工具,可以快速构建应用程序的逻辑和界面。

3. 调试应用程序:在开发过程中,可以使用调试工具对应用程序进行调试,定位和修复程序中的错误和异常。

App.Config详解

App.Config详解

App.Config详解应用程序配置文件是标准的XML 文件,XML 标记和属性是区分大小写的。

它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。

配置文件的根节点是configuration。

我们经常访问的是appSettings,它是由.Net预定义配置节。

我们经常使用的配置文件的架构是象下面的形式。

先大概有个印象,通过后面的实例会有一个比较清楚的认识。

下面的“配置节”可以理解为进行配置一个XML的节点。

1. 向项目添加app.config文件:右击项目名称,选择“添加”→“添加新建项”,在出现的“添加新项”对话框中,选择“添加应用程序配置文件”;如果项目以前没有配置文件,则默认的文件名称为“app.config”,单击“确定”。

出现在设计器视图中的app.config文件为:<?xml version="1.0 "encoding= "utf-8 " ?><configuration></configuration>在项目进行编译后,在bin/Debuge文件下,将出现两个配置文件(以本项目为例),一个名为“Jxc Management.EXE.config”,另一个名为“JxcManagement.vshost.exe.config”。

第一个文件为项目实际使用的配置文件,在程序运行中所做的更改都将被保存于此;第二个文件为原代码“app.config”的同步文件,在程序运行中不会发生更改。

2. connectionStrings配置节:请注意:如果您的SQL版本为2005 Express版,则默认安装时SQL服务器实例名为localhost/SQLExpress,须更改以下实例中“Data Source=localhost;”一句为“Data Source=localhost/SQLExpress;”,在等于号的两边不要加上空格。

详解ASP.NET配置文件Web.config

详解ASP.NET配置文件Web.config

详解配置⽂件Web.configWeb.config⽂件是⼀个XML⽂本⽂件,它⽤来储存 Web 应⽤程序的配置信息(如最常⽤的设置 Web 应⽤程序的⾝份验证⽅式),它可以出现在应⽤程序的每⼀个⽬录中。

当你通过新建⼀个Web应⽤程序后,默认情况下会在根⽬录⾃动创建⼀个默认的Web.config⽂件,包括默认的配置设置,所有的⼦⽬录都继承它的配置设置。

如果你想修改⼦⽬录的配置设置,你可以在该⼦⽬录下新建⼀个Web.config⽂件。

它可以提供除从⽗⽬录继承的配置信息以外的配置信息,也可以重写或修改⽗⽬录中定义的设置。

在运⾏时对Web.config⽂件的修改不需要重启服务就可以⽣效(注:<processModel> 节例外)。

当然Web.config⽂件是可以扩展的。

你可以⾃定义新配置参数并编写配置节处理程序以对它们进⾏处理。

⼀、web.config配置⽂件(默认的配置设置)以下所有的代码都应该位于<configuration><system.web>和</system.web></configuration>之间,出于学习的⽬的下⾯的⽰例都省略了这段XML标记1、<authentication> 节作⽤:配置 ⾝份验证⽀持(为Windows、Forms、PassPort、None四种)。

该元素只能在计算机、站点或应⽤程序级别声明。

<authentication> 元素必需与<authorization> 节配合使⽤。

⽰例:以下⽰例为基于窗体(Forms)的⾝份验证配置站点,当没有登陆的⽤户访问需要⾝份验证的⽹页,⽹页⾃动跳转到登陆⽹页。

<authentication mode="Forms" ><forms loginUrl="logon.aspx" name=".FormsAuthCookie"/></authentication>其中元素loginUrl表⽰登陆⽹页的名称,name表⽰Cookie名称2、<authorization> 节作⽤:控制对 URL 资源的客户端访问(如允许匿名⽤户访问)。

asp.net在类库中使用EF6.0时的相关配置

asp.net在类库中使用EF6.0时的相关配置

在类库中使⽤EF6.0时的相关配置前提:之前使⽤EF的配置都是直接使⽤NuGet安装在项⽬中,然后直接修改web.config中的connectionString,然后创建相关dbcontext直接使⽤就可以了。

此次为直接将EF安装在相关类库中进⾏使⽤。

1.使⽤NuGet将EF安装到所需的类库中。

会⾃动添加引⽤和App.config与packafes.config⽂件。

2.修改App.config⽂件进⾏配置:<?xml version="1.0" encoding="utf-8"?><configuration><configSections><section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections><entityFramework><providers>//配置针对于Sql server的provider<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /></providers></entityFramework><connectionStrings>//配置相关connectionString<add name="MyContext"providerName="System.Data.SqlClient"connectionString="Server=xxx;Database=xxx;Integrated Security=False;User ID=sa;Password=xxx;"/></connectionStrings></configuration>3.配置⾃⼰的dbcontext:public class MyContext : DbContext{public MyContext(): base("MyContext"){}public DbSet<T> TNames{ get; set; }public DbSet<T> TNames{ get; set; }}4.可以正常使⽤EF相关功能。

使用.NET配置文件appSettings元素的File属性

使⽤.NET配置⽂件appSettings元素的File属性由于 Web.config ⽂件的任何更改都会导致应⽤程序重新启动,因此,使⽤⼀个单独的⽂件则既可以让⽤户修改appSettings节中的值,⼜不会导致应⽤程序重新启动。

该单独⽂件的内容将与 Web.config ⽂件中的 appSettings 节合并。

此功能限于 appSettings 属性。

注意在 .NET Framework 2.0 版中,现在可以在⼀个单独⽂件包括所有⽀持 configSource 属性的配置元素的配置设置。

但是,当使⽤ configSource 属性时,由于没有元素设置的合并,因此您必须将整个节移动到单独⽂件。

使⽤ configSource 属性时,对Web.config ⽂件有⼀次写⼊操作。

这会导致应⽤程序重新启动,但是随后对该节的更新会直接写⼊单独⽂件,⽽不会导致后⾯的应⽤程序重新启动。

有关更多信息,请参见。

If you need to share configuration settings among multiple .NET assemblies, the practice of maintaining separate config files for each assembly can quickly become tedious. For instance, if you have multiple executables within a directory that all need a 'ConnectionString' entry in their config file, the traditional method in .NET would be for each executable to have its own config file. This can become a burden in an enterprise environment when you need to change the connection string, as you would be forced to change each individual config file. Fortunately, there is a better approach. This better approach involves using the File attribute of the .NET config file's appSettings element. This attribute should contain the relative path to a custom config file, which all other applications can share. The description from MSDN on the appSettings File attribute follows:如果你需要在多个.NET程序集之间共享配置设置,那么维护每⼀个程序集的配置⽂件会很快使你觉得很烦闷。

Config程序配置文件(configSections)操作实践及代码详注

Config程序配置⽂件(configSections)操作实践及代码详注所有与配置⽂件相关的类:(粗体为⼀般情况下使⽤到的类,其它类功能可能在很复杂的情况下才使⽤到。

)1、ConfigurationManager,这个提供⽤于打开客户端应⽤程序集的Configuration对象。

2、WebConfigurationMaManager,这个提供⽤于打开web应⽤程序集的Configuration对象。

3、ConfigurationSection ,表⽰配置⽂件中的区域对象。

4、ConfigurationSectionCollection ,表⽰配置⽂件中相关区域的集合。

5、ConfigurationSectionGroup ,表⽰配置⽂件中的⼀组相关区域的组对象。

6、ConfigurationSectionGroupCollection ,表⽰ ConfigurationSectionGroup 对象的集合。

7、ConfigurationProperty ,表⽰区域或元素的属性。

8、ConfigurationPropertyAttribute ,以声明⽅式指⽰ .NET Framework,以实例化配置属性。

9、ConfigurationElement ,表⽰配置⽂件中的元素对象。

10、ConfigurationElementCollection ,表⽰元素的集合的对象。

⽂章中只对粗体的类进⾏实践,因为已经可以涵盖80%以上的需求。

使⽤的需求环境:对于在程序中⽤到的⼀些参数配置可能会随着程序的使⽤⽽改变,如果将这些参数写在代码⾥并编译到EXE⽂件中,那这些参数的改变则⽆法得到保存。

如果下次程序启动的时候想载⼊改变后的参数配置则必须将这些参数配置写⼊到⼀个⽂件中保存。

.NET中提供了⼀个System.Configuration.dll,这个命名空间下提供的类可以很⽅便的把这些参数配置读写到XML⽂件中。

winform程序读取和改写配置文件App.config元素的值

winform程序读取和改写配置⽂件App.config元素的值 在开发Web项⽬的时候,会有⼀个配置⽂件Web.config,⽤来存放⼀些全局的变量,如连接数据库⽤的字符串。

相应的,在开发winform程序时,也有⼀个配置⽂件,它就是App.config,这个⽂件的作⽤与Web.config⼤致相同,也可以⽤来存放程序所⽤的全局变量及Value值。

来看⼀个app.config⽂件的例⼦:<?xml version="1.0" encoding="utf-8" ?><configuration><appSettings><!--图⽚存放路径--><add key="ImgPath" value="D:\img\"/></appSettings></configuration>可以看出,app.config和web.config⼀样,嗯,它也是⼀个XML⽂件。

那怎么对这个⽂件中的元素进⾏读取操作呢?很简单,来看代码:string strPath = System.Configuration.ConfigurationSettings.AppSettings["ImgPath"].ToString();这样就可以把app.config⽂件中ImgPath这个元素的Value值读取出来了。

那怎么改写元素的值呢?如果你认为像读那样的去写,像这样的代码:System.Configuration.ConfigurationSettings.AppSettings["ImgPath"] = @"E:\img\"; //这样写是没⽤的在对app.config⽂件的元素Value值进⾏修改操作时,只能把app.config⽂件当作⼀个普通的XML⽂件来对待,利⽤System.Xml.XmlDocument类把这个app.config⽂件读到内存中,并通过System.Xml.XmlNode类找到appSettings节点,通过System.Xml.XmlElement类找到节点下的某个元素,利⽤SetAttribute⽅法来修改这个元素的值后,最后再将app.config⽂件保存到原的⽬录中,这样,才算完成了对⼀个元素Value值的修改操作。

八、.netcore(.NET6)配置读取appsettings文件内容的通用功能

⼋、.netcore(.NET6)配置读取appsettings⽂件内容的通⽤功能添加通⽤读取配置⽂件功能在Wsk.Core.Package项⽬下,新增Microsoft.Extensions.Configuration包:在启动项⽬下,设置appsettings.json属性为始终复制:新建⼀个⽂件夹Common,⽤于存放⼯具类项⽬。

并且新建项⽬:Wsk.Core.AppSettings,引⽤package包项⽬,然后新建⼀个读取配置⽂件的通⽤类,叫AppHelper。

⽬录结构如图:在AppHelper类⾥⾯,新建静态操作⽅法有关代码,⽤于读取根⽬录下的配置⽂件信息:代码:public class AppHelper{private static IConfiguration _config;public AppHelper(IConfiguration configuration){_config = configuration;}///<summary>///读取指定节点的字符串///</summary>///<param name="sessions"></param>///<returns></returns>public static string ReadAppSettings(params string[] sessions){try{if (sessions.Any()){return _config[string.Join(":", sessions)];}}catch{return"";}return"";}///<summary>///读取实体信息///</summary>///<typeparam name="T"></typeparam>///<param name="session"></param>///<returns></returns>public static List<T> ReadAppSettings<T>(params string[] session) {List<T> list = new List<T>();_config.Bind(string.Join(":", session), list);return list;}}View Code在启动项⽬下,新建⽂件夹ConfigServices,⽤于存放各种服务的添加项⽬。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一步一步教你玩转.NET Framework的配置文件app.config在一般的项目中,为了使你的代码更加灵活,更方便调整,减少不必要的hard code,我们都在config中添加许多配置信息,一般可以选择.NET自带的配置文件形式app.config或者web 项目中的web.config来完成配置工作。

.NET中提供了几个和配置有关的类来支持用完轻松的完成配置文件的读写设置:System.Configuration.ConfigurationSectionGroup一般和你项目中使用的Assambly保持1:1的对应关系,这样划分使得结构相对清晰,权责明确。

当然你可以不使用它,这样一旦你的Assambly在别的地方要被重用时,找出相应的config信息就变得很困难。

System.Configuration.ConfigurationSection维护一个相对独立的配置节,使用时需现在<ConfigSections></ConfigSections>节点下声明。

我们熟悉的<appSettings></appSettings>以及<connectionStrings></connectionStrings/>就是.NET为我们预留的一个Section。

System.Configuration.ConfigurationElementCollection &System.Configuration.ConfigurationElement就是Section下具体的配置信息和配置信息的集合了。

下面来看看怎么使用这些类玩转app.config1.初级玩法最初级的用法当然是使用<appSettings/>,我们在app.config 中添加<configuration><appSettings><add key="MyConfigString" value="Test Config Data"/></appSettings></configuration>访问它public class AppSettingConfig{public string resultValue;public AppSettingConfig(){this.resultValue =ConfigurationManager.AppSettings["MyConfigString"].ToString();}}[TestMethod]public void TestAppSettingConfigNode(){AppSettingConfig appCon = new AppSettingConfig();Assert.AreEqual("Test Config Data", appCon.resultValue);}没有问题!我们加个Section来看看如何访问:<configuration><configSections><sectionGroup name="MySectionGroup"><section name="MyFirstSection"type="System.Configuration.DictionarySectionHandler"/><section name="MySecondSection"type="System.Configuration.DictionarySectionHandler"/></sectionGroup></configSections><MySectionGroup><MyFirstSection><add key="First" value="First Section"/></MyFirstSection><MySecondSection><add key="Second" value="Second Section"/></MySecondSection></MySectionGroup></configuration>注意我们在section的type中给出了System.Configuration.DictionarySectionHandler,这也限制了我们在具体的ConfigurationElement中只能使用<add key=”” value=””/>的形式,使得我们GetSection()方法返回的是一个IDictory对象,我们可以根据Key来取得相应的值public class SectionConfig{public string resultValue;public SectionConfig(){System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None );IDictionary dic =ConfigurationManager.GetSection("MySectionGroup/MySecondSection") as IDictionary;this.resultValue = dic["Second"].ToString();}}[TestMethod]public void TestSectionGroupConfigNode(){SectionConfig sc = new SectionConfig();Assert.AreEqual("First Section", sc.resultValue);}还是没问题。

2. 中级玩法.NET支持对上述提到的configuration类进行扩展,我们可以定义自己的Section。

继承自基类System.Configuration.ConfigurationSection,ConfigurationSection已经提供了索引器用来获取设置数据。

在类中加上ConfigurationProperty属性来定义Section中的Element:public class CustomSection:System.Configuration.ConfigurationSection{[ConfigurationProperty("sectionId", IsRequired=true,IsKey=true)]public int SectionId {get { return (int)base["sectionId"]; }set { base["sectionId"] = value; }}[ConfigurationProperty("sectionValue", IsRequired = false)] public string SectionValue {get { return base["sectionValue"].ToString(); }set { base["sectionValue"] = value; }}}操作此Section,我们将其动态加入app.config中,并读出来:public class CustomSectionBroker{private CustomSection customSection = null;public void InsertCustomSection(){customSection = new CustomSection();customSection.SectionId = 1;customSection.SectionValue = "The First Value";System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None );config.Sections.Add("CustomSection", customSection);config.Save(ConfigurationSaveMode.Minimal);}public int GetCustomSectionID(){System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None );CustomSection cs = config.GetSection("CustomSection") as CustomSection;return cs.SectionId;}}[TestMethod]public void TestCustomSection(){CustomSectionBroker cb = new CustomSectionBroker();cb.InsertCustomSection();Assert.AreEqual(1, cb.GetCustomSectionID());}可以看下现在app.config文件的变化:<configuration><configSections><section name="CustomSection"type="Tonnie.Configuration.Library.CustomSection, Tonnie.Configuration.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /><sectionGroup name="MySectionGroup"><section name="MyFirstSection"type="System.Configuration.DictionarySectionHandler"/><section name="MySecondSection"type="System.Configuration.DictionarySectionHandler"/></sectionGroup></configSections><CustomSection sectionId="1" sectionValue="The First Value" /><MySectionGroup><MyFirstSection><add key="First" value="First Section"/></MyFirstSection><MySecondSection><add key="Second" value="Second Section"/></MySecondSection></MySectionGroup></configuration>增加了一个单独的Section,名为"CustomSection",并且包含了我们创建的2个configurationProperty。

相关文档
最新文档