如何在安装时设置应用程序设置(通过安装程序类)

发布于 2024-07-14 01:53:35 字数 429 浏览 9 评论 0原文

我有一个包含 Installer 类的 Visual Studio 安装项目。 在安装程序类中,我设置了如下设置:

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

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

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

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

消息框确实表明正在设置该值,但是当我转到 .config 文件时,该值仍然是空白!

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

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!!

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

饮惑 2024-07-21 01:53:35

我为安装程序所做的就是使用 App.Config 中的“文件”属性。 appSettings 块采用“文件”属性,如下所示:

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

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

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

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 方法中添加类似以下内容:

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 个小时,包括检查多个配置文件来设置密码。 现在它几乎完全自动化了。

希望这可以帮助。

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.

独闯女儿国 2024-07-21 01:53:35

最后我放弃了,并在安装应用程序后使用 RunOnce 类型的方法来执行此操作。

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

不念旧人 2024-07-21 01:53:35

老实说,我不知道安装程序期间是否支持此功能 - 但如果支持,请确保您在 Settings.Default 上调用 Save()

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.

咆哮 2024-07-21 01:53:35

简而言之,安装程序类不支持它。 您只需要了解安装程序类方法是从系统目录运行的 msiexec.exe 调用的,并且该环境不可能知道您在目录中的某个位置有一个它完全不知道的设置文件。 这就是为什么它使用显式转到文件的安装位置并在那里更新它的代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文