Csharp/C#教程:加载Properties.Settings在运行时从不同的文件分享


加载Properties.Settings在运行时从不同的文件

有没有办法在运行时从默认App.config文件以外的其他文件加载设置? 我想在加载默认配置文件后执行此操作。

我使用Visual Studio中的Settings.Settings GUI为我创建我的App.config文件。 配置文件最终看起来像这样:

      
1234

在代码中,我可以访问如下设置:

 Console.WriteLine("Default setting value: " + Properties.Settings.Default.SettingSomething); 

我的想法是,在运行应用程序时,我应该能够在运行时指定配置文件,并让应用程序将配置文件加载到Properties.Settings.Default对象中,而不是使用默认的app.config文件。 配置文件的格式相同,但设置的值会有所不同。

我知道使用ConfigurationManager.OpenExeConfiguration(configFile);执行此操作的方法ConfigurationManager.OpenExeConfiguration(configFile); 。 但是,在我运行的测试中,它不会更新Properties.Settings.Default对象以反映配置文件中的新值。


在考虑了这个问题之后,我已经能够提出一个我更喜欢的解决方案。 我确信它有一些陷阱,但我认为这对我需要做的事情有用。

本质上, Properties.Settings类由Visual Studio自动生成; 它为您生成类的代码。 我能够找到代码生成的位置,并添加一些函数调用来自行加载配置文件。 这是我的补充:

 internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { //Parses a config file and loads its settings public void Load(string filename) { System.Xml.Linq.XElement xml = null; try { string text = System.IO.File.ReadAllText(filename); xml = System.Xml.Linq.XElement.Parse(text); } catch { //Pokemon catch statement (gotta catch 'em all) //If some exception occurs while loading the file, //assume either the file was unable to be read or //the config file is not in the right format. //The xml variable will be null and none of the //settings will be loaded. } if(xml != null) { foreach(System.Xml.Linq.XElement currentElement in xml.Elements()) { switch (currentElement.Name.LocalName) { case "userSettings": case "applicationSettings": foreach (System.Xml.Linq.XElement settingNamespace in currentElement.Elements()) { if (settingNamespace.Name.LocalName == "SnipetTester.Properties.Settings") { foreach (System.Xml.Linq.XElement setting in settingNamespace.Elements()) { LoadSetting(setting); } } } break; default: break; } } } } //Loads a setting based on it's xml representation in the config file private void LoadSetting(System.Xml.Linq.XElement setting) { string name = null, type = null, value = null; if (setting.Name.LocalName == "setting") { System.Xml.Linq.XAttribute xName = setting.Attribute("name"); if (xName != null) { name = xName.Value; } System.Xml.Linq.XAttribute xSerialize = setting.Attribute("serializeAs"); if (xSerialize != null) { type = xSerialize.Value; } System.Xml.Linq.XElement xValue = setting.Element("value"); if (xValue != null) { value = xValue.Value; } } if (string.IsNullOrEmpty(name) == false && string.IsNullOrEmpty(type) == false && string.IsNullOrEmpty(value) == false) { switch (name) { //One of the pitfalls is that everytime you add a new //setting to the config file, you will need to add another //case to the switch statement. case "SettingSomething": this[name] = value; break; default: break; } } } } 

我添加的代码公开了一个Properties.Settings.Load(string filename)函数。 该函数接受配置文件名作为参数。 它将解析文件并加载它在配置文件中遇到的任何设置。 要恢复到原始配置,只需调用Properties.Settings.Reload()

希望这可能会帮助别人!

这取决于应用程序的类型:

  1. Web应用程序和Windows应用程序 – 如果您愿意将配置文件存储在与应用程序相同的文件夹(或子文件夹)中,请使用configSource xml属性
  2. 创建设置提供程序并实现IApplicationSettingsProvider 。 样品在这里和这里 。 您可能还需要使用IConfigurationManagerInternal接口来替换默认的.NET配置管理器。 在实施提供程序时,不要忘记在用户设置和应用程序设置以及漫游配置文件之间进行区分。

如果您想快速入门,只需反编译LocalFileSettingsProvider类(默认设置提供程序)并根据需要进行更改(您可能会发现一些useles代码,可能需要复制它所依赖的所有类)。

祝好运

查看使用ExeConfigurationFileMap和ConfigurationManager.OpenMappedExeConfiguration。

请参阅破解.Net 2.0配置的秘密

ExeConfigurationFileMap允许您在调用OpenMappedExeConfiguration()时专门配置机器,exe,漫游和本地配置文件的确切路径名,一起或零碎。 您不需要指定所有文件,但在创建Configuration对象时将识别并合并所有文件。 使用OpenMappedExeConfiguration时,重要的是要了解通过您请求的级别的所有级别的配置将始终合并。 如果指定自定义exe和本地配置文件,但未指定计算机和漫游文件,则将找到默认计算机和漫游文件,并将其与指定的exe和用户文件合并。 如果指定的文件未与默认文件同步,则可能会产生意外后果。

您可以包含类型,因此您不需要每次都手动更新源。

`private void LoadSetting(System.Xml.Linq.XElement setting){string name = null,type = null; string value = null;

上述就是C#学习教程:加载Properties.Settings在运行时从不同的文件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

  if (setting.Name.LocalName == "setting") { System.Xml.Linq.XAttribute xName = setting.Attribute("name"); if (xName != null) { name = xName.Value; } System.Xml.Linq.XAttribute xSerialize = setting.Attribute("serializeAs"); if (xSerialize != null) { type = xSerialize.Value; } System.Xml.Linq.XElement xValue = setting.Element("value"); if (xValue != null) { if (this[name].GetType() == typeof(System.Collections.Specialized.StringCollection)) { foreach (string s in xValue.Element("ArrayOfString").Elements()) { if (!((System.Collections.Specialized.StringCollection)this[name]).Contains(s)) ((System.Collections.Specialized.StringCollection)this[name]).Add(s); } } else { value = xValue.Value; } if (this[name].GetType() == typeof(int)) { this[name] = int.Parse(value); } else if (this[name].GetType() == typeof(bool)) { this[name] = bool.Parse(value); } else { this[name] = value; } } }` 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1029463.html

(0)
上一篇 2022年1月12日
下一篇 2022年1月12日

精彩推荐