从c#编辑配置文件的基址
本文关键字:配置文件 编辑 | 更新日期: 2023-09-27 18:16:55
我有一个使用WCF服务的应用程序。我需要从应用程序编辑baseAddress。如何从c#访问baseAddress。app.config的部分附在下面,请帮忙!目标是在代码中获取baseAddresses和http://192.xx.xx.xx/Api/TEST_API.asmx值。
<system.serviceModel>
<services>
<service name="WCF.Service1" behaviorConfiguration=".Service1.Service1Behavior">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding" contract="WCF.IService1" name="NetTcpBinding">
</endpoint>
<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="WCF.IService1" name="BasicHttpBinding">
</endpoint>
<!--<endpoint address="rest"
binding="webHttpBinding"
contract="WCF.IService1" behaviorConfiguration="webby">
</endpoint>-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8084/api/" />
<add baseAddress="net.tcp://localhost:8083/api/" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding">
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webby">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WCF.Service1.Service1Behavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v2.0.50727" />
</startup>
<applicationSettings>
<TEST.Properties.Settings>
<setting name="APP_WebReference_API" serializeAs="String">
<value>http://192.xx.xx.xx/Api/TEST_API.asmx</value>
</setting>
</TEST.Properties.Settings>
</applicationSettings>
</configuration>
像这样:
// read the old value
XmlDocument doc = new XmlDocument();
doc.Load(pathToConfig + "app.config");
XmlNode root = doc.DocumentElement;
XmlNode myNode = root.SelectSingleNode(@"applicationSettings/TEST.Properties.Settings/setting[@name='APP_WebReference_API']");
var oldValue = myNode.InnerText;
// save the new value
myNode.Value = @"http://blah.blah.com/Api/TEST_API.asmx";
doc.Save(pathToConfig + "app.config");