因為我們系統在區域網路中發布的時候IP地址可能會發生變動所以我打算在程式中增加一個修改IP地址功能,連接埠找一個稍微冷門一點的連接埠但是IP地址是可以修改的。我查了一下資料說是要從config檔案中修改這裡要說一下我們C#編譯完成之後有兩個設定檔第一個是vshost.config檔案,第二個是config檔案,第二個是release設定檔,第二個是dubug設定檔。設定檔格式如下所示:
<?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測試程式.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <applicationSettings> <WebService測試程式.Properties.Settings> <setting name="WebService測試程式_WebReference1_Test1_Function" serializeAs="String"> <value>http://10.68.69.203:8081/TWS.asmx</value> </setting> <setting name="WebService測試程式_WebReference2_Test1_Function" serializeAs="String"> <value>http://10.68.69.203:8082/TWS.asmx</value> </setting> </WebService測試程式.Properties.Settings> </applicationSettings></configuration>
我現在要做的就是要修改那個value的值使程式在執行的時候動態去修改這個值然後調用webservice
修改的函數:
/// <summary> /// 修改設定檔 /// </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(); //獲得設定檔的全路徑 string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; doc.Load(strFileName); XmlNodeList nodes = doc.GetElementsByTagName("setting"); foreach (XmlNode servernode in nodes) { XmlElement xe1 = (XmlElement)servernode; XmlNodeList serverchildnodes = xe1.ChildNodes; foreach (XmlNode childnode in serverchildnodes) { if (childnode.Name == "value") { //childnode.InnerText = "http://10.68.69.203:8081/TWS.asmx"; childnode.InnerText = Str_NewURl; } } } //儲存上面的修改 doc.Save(strFileName); //this.Txt1.Text = DynamicURL.LoadURL(); Int_Return = 1; return Int_Return; }
修改完成之後調用就可以了!