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!