如何在安装时设置应用程序设置(通过安装程序类) [英] How can I set application settings at install time (via installer class)

查看:79
本文介绍了如何在安装时设置应用程序设置(通过安装程序类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有Installer类的Visual Studio安装项目.在安装程序类中,我进行了如下设置:

I have a Visual Studio setup project that has an Installer class. In the installer class I set a setting as follows:

MessageBox.Show(Properties.Settings.Default.MySetting);

Properties.Settings.Default.MySetting = "Foo";
Properties.Settings.Default.Save();

MessageBox.Show(Properties.Settings.Default.MySetting);

问题是,即使我知道此代码正在执行(我在做其他事情),也从未设置该设置!

The problem is that even though I know that this code is being executed (I am doing other stuff), the setting is never set!!

消息框的确提示正在设置该值,但是当我转到 .config 文件时,该值仍为空白!

The message boxes do suggest that the value is being set, but when I go to the .config file the value is still blank!

任何人都知道为什么和/或可能的解决方法吗?

Anyone have any ideas why and/or a possible workaround?

推荐答案

我为安装程序所做的是使用App.Config中的文件"属性.appSettings块具有文件"属性,如下所示:

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>

文件"属性有点像CSS,因为最具体的设置会获胜.如果在user.config和App.config中定义了"foo",则使用user.config中的值.

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.

然后,我有一个配置生成器,它使用字典中的值将第二个appSettings块写出到user.config(或任何您想调用的块).

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();
            }
        }
    }
}

在安装程序中,只需在Install方法中添加如下内容:

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);

我们将其用于测试,培训和生产服务器,因此我们要做的就是在安装过程中指定计算机名称和密码,一切都由我们负责.过去这是一个3小时的过程,包括浏览多个配置文件来设置密码.现在,它几乎是完全自动化的.

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.

希望这会有所帮助.

这篇关于如何在安装时设置应用程序设置(通过安装程序类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆