我在的形式app.config文件:如何讀取app.config中定義的屬性的值?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://something.com"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer"
contract="ABC" name="XXX" />
<endpoint address="http://something2.com"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer"
contract="ABC2" name="YYY" />
</client>
</system.serviceModel>
</configuration>
我想在屬性讀取值具有名稱=「XXX」節點端點的「地址」。請告訴我該怎麼做!
(繼續在下面討論marc_s。抱歉,因爲註釋不允許格式化代碼,所以將文本放在這裏) @marc_s:我使用下面的代碼來讀取上面的文件,但它顯示clientSection.Endpoints有0成員(Count = 0)。請幫忙!
public MainWindow()
{
var exeFile = Environment.GetCommandLineArgs()[0];
var configFile = String.Format("{0}.config", exeFile);
var config = ConfigurationManager.OpenExeConfiguration(configFile);
var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config);
var clientSection = wcfSection.Client;
foreach (ChannelEndpointElement endpointElement in clientSection.Endpoints)
{
if (endpointElement.Name == "XXX")
{
var addr = endpointElement.Address.ToString();
}
}
}
你不應該使用* vshost.config」文件 - 這是隻打算如果從運行您的程序是存在在Visual Studio中使用'MyApp.exe.config'而不是!! – 2010-07-01 14:37:33
@marc_s:嗨,我已經更新了我的代碼,但是讀取的端點數仍然爲零。 – 2010-07-02 03:01:17
是的,我可以看到 - 用我的代碼但不要使用'ServiceModelSectionGroup.GetSectionGroup(config);'和下面幾行,而是使用我的代碼('ConfigurationManager.GetSection(....)') - 在我的情況下工作! – 2010-07-02 06:06:34