c# – 如何读取app.config中定义的属性的值?
发布时间:2020-12-15 18:14:53 所属栏目:百科 来源:网络整理
导读:我有一个app.config文件,其形式为: ?xml version="1.0" encoding="utf-8" ? configuration system.serviceModel client endpoint address="http://something.com" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer" contr
我有一个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> 我想读取节点端点的属性“address”的值,其名称为“XXX”.请告诉我该怎么做! (继续讨论与marc_s讨论.抱歉把文字放在这里,因为评论不允许格式化代码) 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(); } } } 解决方法
你真的不需要–WCF运行时将为你做所有这些.
如果你真的必须 – 无论出于何种原因 – 你可以这样做: using System.Configuration; using System.ServiceModel.Configuration; ClientSection clientSettings = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection; string address = null; foreach(ChannelEndpointElement endpoint in clientSettings.Endpoints) { if(endpoint.Name == "XXX") { address = endpoint.Address.ToString(); break; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |