是否有人知道我可以在.Net應用程序中設置應用程序(或用戶)級別設置的方式,這些設置以應用程序當前的開發模式爲條件? IE:調試/發佈取決於配置模式的不同應用程序設置
更具體地說,我有我的應用程序設置中保存的web服務的URL引用。在發佈模式期間,我希望這些設置在調試模式期間指向http://myWebservice.MyURL.com,我希望這些設置爲http://myDebuggableWebService.MyURL.com。
任何想法?
是否有人知道我可以在.Net應用程序中設置應用程序(或用戶)級別設置的方式,這些設置以應用程序當前的開發模式爲條件? IE:調試/發佈取決於配置模式的不同應用程序設置
更具體地說,我有我的應用程序設置中保存的web服務的URL引用。在發佈模式期間,我希望這些設置在調試模式期間指向http://myWebservice.MyURL.com,我希望這些設置爲http://myDebuggableWebService.MyURL.com。
任何想法?
據我所知,還沒有建立這樣做的方式。在我們的項目中,我們維護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.xml
,settings.settings.Release.xm
升等。
Scott Hanselman在描述一個稍微「聰明」的辦法,唯一的區別是,我們沒有檢查,看是否該文件已改變: http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
如果您想要將所有內容保存在一個配置文件中,您可以將自定義配置部分引入到您的app.settings中,以存儲調試和發佈模式的屬性。
您可以將應用程序中的對象保存在存儲開發模式特定設置的應用程序中,也可以覆蓋基於調試開關的現有應用程序。
下面是一個簡單的控制檯應用程序示例(DevModeDependencyTest):
的App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="DevModeSettings">
<section name="debug" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" />
<section name="release" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
<DevModeSettings>
<debug webServiceUrl="http://myDebuggableWebService.MyURL.com" />
<release webServiceUrl="http://myWebservice.MyURL.com" />
</DevModeSettings>
<appSettings>
<add key="webServiceUrl" value="http://myWebservice.MyURL.com" />
</appSettings>
</configuration>
存儲您的自定義配置的對象(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();
}
}
}
我知道這是幾年前問過,但以防萬一有人正在尋找,我用一個簡單而有效的解決方案。
轉到項目屬性,設置選項卡(您會看到您的Web服務URL或任何其他已在此處列出的設置)。
單擊設置頁面上的「查看代碼」按鈕。
在構造函數中輸入它。
this.SettingsLoaded += Settings_SettingsLoaded;
添加下面的函數的構造下:
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
}
現在,只要你運行你的項目,它將編譯只匹配當前構建配置線。
SlowCheetah增加你問不僅爲App.config中但對於項目中的任何XML文件的功能 - http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5
雖然這可能會在理論上回答這個問題,[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包括答案的基本部分,並提供供參考的鏈接。 – Spontifixus 2013-01-28 12:43:48
我認爲這可能是最好的答案 - 它似乎允許每生成配置app.config轉換(很像在web.config轉換生成)。 – 2013-12-06 14:21:58
這有點遲到了,但我偶然發現實施web.transform
的一個很好的方式方法爲app.config
文件。 (即它使用命名空間http://schemas.microsoft.com/XML-Document-Transform
)
我認爲這是「很好」,因爲它是一種純粹的xml方法,不需要第三方軟件。
在我看來這比保持x
一些其中獲得其全部複製配置文件,如在其他的答案更加複雜和強大。
的演練已經張貼在這裏: http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/
看,媽媽 - 我的IDE沒有明確後生成事件!
乾杯隊友完美無缺! – petric 2017-07-05 11:25:54
我有一個類似的問題要解決,結束了使用XDT(web.config中)轉換引擎,已經從ne1410s答案,可以在這裏找到建議:https://stackoverflow.com/a/27546685/410906
但不是這樣做手動在他的鏈接描述我用這個插件:https://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859
該插件只能幫助安裝配置,它並不需要建立和解決方案可以在其他機器或生成服務器上建立無插件或任何需要其他工具。
謝謝hhravn! – 2010-06-22 06:11:42