Updating .config file from an Custom Installer Class Action

后端 未结 5 1266
春和景丽
春和景丽 2021-02-04 20:03

I\'ve tried updating my application\'s .config file during installer (via a .NET Installer Class Action). But, I can\'t seem to get ConfigurationManager to lis

5条回答
  •  我在风中等你
    2021-02-04 20:33

    I came across the same problem, after deep investigation, I found out the easiest way to update any part of config files (e.g. app.config), that's by using XPath. We have an application which connects to web service, during the installation, the user enters the URL of the web service and this should be saved in the following app.config file:

            
        
          
            
              
    whatever comes from setup should go here

    Here is the code to do this in the installer class:

    public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
    
            string targetDirectory = Context.Parameters["targetdir"];
            string param1 = Context.Parameters["param1"];
    
            string path = System.IO.Path.Combine(targetDirectory, "app.config");
    
            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
    
            xDoc.Load(path);
    
            System.Xml.XmlNode node = xDoc.SelectSingleNode("/configuration/applicationSettings/Intellisense.ApplicationServer.Properties.Settings/setting[@name='ApplicationServer_ApplicationServerUrl']/value");
            node.InnerText = (param1.EndsWith("/") ? param1 : param1 + "/");
    
            xDoc.Save(path); // saves the web.config file  
        }
    

    Basically, since the config file is a XML based document, I am using XPath expression to locate specific node and change its value.

提交回复
热议问题