using System; using System.Collections; using System.ComponentModel; using System.Configuration; using System.Configuration.Install; using System.IO; using System.Reflection; using System.Xml; using static System.Collections.Specialized.BitVector32; [RunInstaller(true)] public class ConfigInstaller : Installer { public override void Install(IDictionary stateSaver) { base.Install(stateSaver); string appPath = Context.Parameters["targetdir"]; string apiBaseUrl = Context.Parameters["apibaseurl"]; string configPath = Path.Combine(appPath, "報到系統客戶端.exe.config"); var xml = new XmlDocument(); xml.Load(configPath); var nsmgr = new XmlNamespaceManager(xml.NameTable); // applicationSettings 沒 namespace,不用加 var node = xml.SelectSingleNode( "/configuration/applicationSettings/報到系統客戶端.Properties.Settings" + "/setting[@name='ApiBaseUrl']/value" ); if (node == null) throw new Exception("找不到 ApiBaseUrl"); node.InnerText = apiBaseUrl; xml.Save(configPath); //var config = ConfigurationManager.OpenExeConfiguration(exeConfig); //// 取得 applicationSettings //var appSettings = config.GetSection("applicationSettings/報到系統客戶端.Properties.Settings") as ClientSettingsSection; //if (appSettings == null) // throw new Exception("找不到 applicationSettings/報到系統客戶端.Properties.Settings 區段"); //// 尋找設定值 //SettingElement target = null; //foreach (SettingElement s in appSettings.Settings) //{ // if (s.Name == "ApiBaseUrl") // { // target = s; // break; // } //} //if (target == null) // throw new Exception("找不到 ApiBaseUrl"); //// 寫入設定值 //target.Value.ValueXml.InnerText = apiBaseUrl; //// 存檔 //config.Save(ConfigurationSaveMode.Modified); } }