不同的应用程序设置取决于配置模式

是否有人知道我可以在.Net应用程序中设置应用程序当前开发模式的应用程序(或用户)级别设置? IE:调试/发布

更具体地说,我有一个url引用我的应用程序设置中保存的webservices。 在发布模式期间,我希望这些设置在调试模式期间指向http://myWebservice.MyURL.com我希望这些设置为http://myDebuggableWebService.MyURL.com 。

有任何想法吗?

我知道这是多年前提出来的,但以防万一有人在寻找我使用的简单而有效的解决方案。

  1. 转到项目属性,“设置”选项卡(您将看到您的Web服务URL或此处已列出的任何其他设置)。

  2. 单击“设置”页面上的“查看代码”按钮。

  3. 在构造函数中键入this。

    this.SettingsLoaded += Settings_SettingsLoaded; 
  4. 在构造函数下添加以下函数:

     void Settings_SettingsLoaded(object sender, System.Configuration.SettingsLoadedEventArgs e) { #if(DEBUG) this["YOUR_SETTING_NAME"] = VALUE_FOR_DEBUG_CONFIGURATION; #else this["YOUR_SETTING_NAME"] = VALUE_FOR_RELEASE_CONFIGURATION; #endif } 

现在,无论何时运行项目,它都只编译与当前构建配置匹配的行。

这对派对来说有些晚了,但我偶然发现了一种为app.config文件实现web.transform方法的好方法。 (即它使用命名空间http://schemas.microsoft.com/XML-Document-Transform

我认为这很“好”,因为它是纯xml方法,不需要第三方软件。

  • 根据您的各种构建配置,父/默认App.config文件来自。
  • 这些后代只会覆盖他们需要的东西。

在我看来,这比必须维护x个完整复制的配置文件要复杂得多,比如在其他答案中。

这里发布了一个演练: http : //mitasoft.wordpress.com/2011/09/28/multipleappconfig/


看,妈妈 – 我的IDE中没有明确的构建后事件!

据我所知,没有内置的方法可以做到这一点。 在我们的项目中,我们维护了4个不同的设置文件,并通过在构建的预构建步骤中将每个文件复制到活动文件中来切换它们。

 copy "$(ProjectDir)properties\settings.settings.$(ConfigurationName).xml" "$(ProjectDir)properties\settings.settings" copy "$(ProjectDir)properties\settings.designer.$(ConfigurationName).cs" "$(ProjectDir)properties\settings.Designer.cs" 

几年来,这对我们来说是完美无缺的。 只需更改目标,即可切换整个配置文件。

编辑:文件命名为settings.settings.Debug.xmlsettings.settings.Release.xm l等。

Scott Hanselman描述了一种稍微“聪明”的方法,唯一的区别是我们没有检查文件是否发生了变化: http : //www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

如果要将所有内容保存在一个配置文件中,可以在app.settings中引入自定义配置部分,以存储调试和发布模式的属性。

您可以在应用程序中保留对象,该对象存储开发模式特定设置,也可以基于调试开关覆盖现有应用程序集。

这是一个简短的控制台应用程序示例(DevModeDependencyTest):

App.config:

     

存储自定义配置的对象(DevModeSettings.cs):

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace DevModeDependencyTest { public class DevModeSetting : ConfigurationSection { public override bool IsReadOnly() { return false; } [ConfigurationProperty("webServiceUrl", IsRequired = false)] public string WebServiceUrl { get { return (string)this["webServiceUrl"]; } set { this["webServiceUrl"] = value; } } } } 

用于访问自定义配置设置的处理程序(DevModeSettingsHandler.cs):

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace DevModeDependencyTest { public class DevModeSettingsHandler { public static DevModeSetting GetDevModeSetting() { return GetDevModeSetting("debug"); } public static DevModeSetting GetDevModeSetting(string devMode) { string section = "DevModeSettings/" + devMode; ConfigurationManager.RefreshSection(section); // This must be done to flush out previous overrides DevModeSetting config = (DevModeSetting)ConfigurationManager.GetSection(section); if (config != null) { // Perform validation etc... } else { throw new ConfigurationErrorsException("oops!"); } return config; } } } 

最后你进入控制台应用程序(DevModeDependencyTest.cs)的入口点:

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace DevModeDependencyTest { class DevModeDependencyTest { static void Main(string[] args) { DevModeSetting devMode = new DevModeSetting(); #if (DEBUG) devMode = DevModeSettingsHandler.GetDevModeSetting("debug"); ConfigurationManager.AppSettings["webServiceUrl"] = devMode.WebServiceUrl; #endif Console.WriteLine(ConfigurationManager.AppSettings["webServiceUrl"]); Console.ReadLine(); } } } 

SlowCheetah不仅为App.config添加了您要求的function,还为项目中的任何XML文件添加了这些function – http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

我有一个类似的问题要解决并最终使用XDT(web.config)转换引擎,这已经在ne1410s的答案中提出,可以在这里找到: https ://stackoverflow.com/a/27546685/410906

但不是像他的链接中所描述的那样手动操作,而是使用了这个插件: https : //visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859

该插件仅帮助设置配置,不需要构建,并且可以在其他计算机或构建服务器上构建解决方案,而无需插件或任何其他工具。