How can I set application settings at install time (via installer class)

£可爱£侵袭症+ 提交于 2019-11-29 04:38:00

What I do for my installers is to use the "file" attribute in App.Config. The appSettings block takes a "file" attribute, like so:

<appSettings file="user.config">
    <add key="foo" value="some value unchanged by setup"/>
</appSettings>

The "file" attribute is sort of like CSS, in that the most specific setting wins. If you have "foo" defined in user.config as well as App.config, the value in user.config is used.

Then, I have a config generator that writes out a second appSettings block to user.config (or whatever you want to call it), using values in a dictionary.

using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Utils
{
    public class ConfigGenerator
    {
        public static void WriteExternalAppConfig(string configFilePath, IDictionary<string, string> userConfiguration)
        {
            using (XmlTextWriter xw = new XmlTextWriter(configFilePath, Encoding.UTF8))
            {
                xw.Formatting = Formatting.Indented;
                xw.Indentation = 4;
                xw.WriteStartDocument();
                xw.WriteStartElement("appSettings");

                foreach (KeyValuePair<string, string> pair in userConfiguration)
                {
                    xw.WriteStartElement("add");
                    xw.WriteAttributeString("key", pair.Key);
                    xw.WriteAttributeString("value", pair.Value);
                    xw.WriteEndElement();
                }

                xw.WriteEndElement();
                xw.WriteEndDocument();
            }
        }
    }
}

In your installer, just add something like the following in your Install method:

string configFilePath = string.Format("{0}{1}User.config", targetDir, Path.DirectorySeparatorChar);

IDictionary<string, string> userConfiguration = new Dictionary<string, string>();

userConfiguration["Server"] = Context.Parameters["Server"];
userConfiguration["Port"] = Context.Parameters["Port"];

ConfigGenerator.WriteExternalAppConfig(configFilePath, userConfiguration);

We use it for our test, training, and production servers, so all we have to do is specify the machine name and password during the install, and everything's taken care of for us. It used to be a 3-hour process, including going through multiple config files to set passwords. Now it's almost entirely automated.

Hope this helps.

Well in the end I gave up and had a RunOnce type of method to do this stuff after the app was installed.

Marc Gravell

I honestly don't know if this is supported during an installer - but if it is, make sure you're calling Save() on Settings.Default.

The short answer is that it's not supported in installer classes. You just need to understand that installer class methods are called from msiexec.exe running from the system directory, and that environment can't possibly know that you have a settings file somewhere in a directory that it is completely unaware of. That's why it works with code that explicitly goes to the installed location of the file and updates it there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!