C # modify the config file to implement dynamic WebService calling

Source: Internet
Author: User

Because the IP address of our system may change when it is released in the LAN, I plan to add a function to modify the IP address in the program, find a port that is a little unpopular, but the IP address can be modified. I checked the information and said that it was to be modified from the config file. Here we should talk about C #. After compilation, there are two configuration files. The first one is vshost. config file, the second is the config file, the second is the release configuration file, and the second is the dubug configuration file. The configuration file format is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <sectiongroup name = "applicationsettings" type = "system. configuration. applicationsettingsgroup, system, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "> <section name =" WebService test program. properties. settings "type =" system. configuration. clientsettingssection, system, version = 2.0.0.0, culture = neutral, publickeytoken = token "requirepermission =" false "/> </sectiongroup> </configsections Settings> <WebService test program. properties. settings> <setting name = "WebService test_webreference1_test1_function" serializeas = "string"> <value> http: // 10.68.69.203: 8081/TWS. asmx </value> </setting> <setting name = "WebService test_webreference2_test1_function" serializeas = "string"> <value> http: // 10.68.69.203: 8082/TWS. asmx </value> </setting> </WebService test program. properties. settings> </applicationsettings> </configuration>

What I want to do now is to modify the value of that value so that the program can dynamically modify this value during execution and then call WebService.

Modified functions:

/// <Summary> /// modify the configuration file /// </Summary> /// <Param name = "str_oldurl"> </param> /// <Param name = "str_newurl"> </param> // <returns> </returns> Public int function_changeconfig (string str_newurl) {int int_return = 0; xmldocument Doc = new xmldocument (); // obtain the full path of the configuration file string strfilename = appdomain. currentdomain. setupinformation. configurationfile; Doc. load (strfilename); xmlnodelist nodes = Doc. trim ("setting"); foreach (xmlnode servernode in nodes) {xmlelement xe1 = (xmlelement) servernode; xmlnodelist serverchildnodes = equals; foreach (xmlnode childnode in serverchildnodes) {If (childnode. name = "value") {// childnode. innertext = "http: // 10.68.69.203: 8081/TWS. asmx "; childnode. innertext = str_newurl ;}}// Save the above modified Doc. save (strfilename); // This. txt1.text = dynamicurl. loadurl (); int_return = 1; return int_return ;}

After the modification, you can call it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.