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

C# 如何在安装时设置应用程序设置(通过installer类),c#,setup-project,appsettings,C#,Setup Project,Appsettings,我有一个VisualStudio安装项目,它有一个安装程序类。在installer类中,我设置了如下设置: MessageBox.Show(Properties.Settings.Default.MySetting); Properties.Settings.Default.MySetting = "Foo"; Properties.Settings.Default.Save(); MessageBox.Show(Properties.Settings.Default.MySetting);

我有一个VisualStudio安装项目,它有一个安装程序类。在installer类中,我设置了如下设置:

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

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

MessageBox.Show(Properties.Settings.Default.MySetting);
问题是,尽管我知道这段代码正在执行(我正在做其他事情),但设置从未设置

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


有人知道原因和/或可能的解决方法吗?

我真的不知道在安装过程中是否支持此功能,但如果支持,请确保您正在调用
Save()
设置中。默认设置

最后,我放弃了,在安装应用程序后,我有了一种RunOnce类型的方法来完成这项工作。

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

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

“file”属性有点像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();
            }
        }
    }
}
使用System.Collections.Generic;
使用系统文本;
使用System.Xml;
命名空间Utils
{
公共类配置生成器
{
公共静态void WriteExternalAppConfig(字符串配置文件路径,IDictionary用户配置)
{
使用(XmlTextWriter xw=新的XmlTextWriter(configFilePath,Encoding.UTF8))
{
xw.Formatting=格式化.缩进;
压痕=4;
xw.WriteStartDocument();
xw.WriteStarteElement(“应用设置”);
foreach(userConfiguration中的KeyValuePair对)
{
xw.书面声明(“添加”);
xw.WriteAttributeString(“key”,pair.key);
WriteAttributeString(“value”,pair.value);
xw.WriteEndElement();
}
xw.WriteEndElement();
xw.WriteEndDocument();
}
}
}
}
在安装程序中,只需在安装方法中添加如下内容:

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);
string configFilePath=string.Format(“{0}{1}User.config”,targetDir,Path.directoryseportorchar);
IDictionary userConfiguration=新字典();
用户配置[“服务器”]=上下文。参数[“服务器”];
用户配置[“端口”]=上下文。参数[“端口”];
WriteExternalAppConfig(configFilePath,userConfiguration);
我们将其用于测试、培训和生产服务器,因此我们所要做的就是在安装过程中指定机器名和密码,一切都会为我们解决。它过去是一个3小时的过程,包括通过多个配置文件来设置密码。现在它几乎完全自动化了


希望这有帮助。

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

我已经设置了保存设置,但尽管如此,值并没有真正被设置!谢谢你,这看起来是我想要的解决方案。尽管我遇到了一个问题,但如果我在安装程序中重写Install方法,则来自自定义UI的值不在上下文参数中。我用的方法对吗?请注意,我没有在CustomActions编辑屏幕中从UI传递数据。