我要如何改变上安装.NET应用/用户设置?我要、用户、NET

2023-09-07 00:57:55 作者:言多必失

在一个Windows服务项目,用项目安装我试过如下:

In a Windows Service project, with a Project Installer I tried the following:

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
    public ProjectInstaller()
    {
        InitializeComponent();
    }

    protected override void OnBeforeInstall(System.Collections.IDictionary savedState)
    {
        base.OnBeforeInstall(savedState);
        Settings.Default.ASetting = "aValue";
        Settings.Default.Save();
    }

    protected override void OnAfterInstall(System.Collections.IDictionary savedState)
    {
        base.OnAfterInstall(savedState);
        Settings.Default.ASetting = "aValue";
        Settings.Default.Save();
    }
}

但是,当我检查config文件安装后,一个旧的价值依然存在。有中没有config文件通常[userfolder] \应用程序数据\本地

But after the installation when I check the .config file, a older value is still there. There was no .config file in the usual [userfolder]\AppData\Local

对于我来说是非常重要的变量定义在安装的时候,因为我会从安装项目的用户输入接收它的价值。这里的恒定值仅用于测试目的。

For me is important to define this variable in installation time since I will receive its value from a user input in the Setup Project. The constant value here is used for testing purposes only.

推荐答案

该框架将不会允许您更改在安装设置,因为是只读的应用程序设置,直到服务已安装并运行没有用户上下文(在用户名)。

The framework will not allow you to change the settings while installing, since Application settings are read-only and there is no user context until the service is installed and running (under a username).

我发现的唯一的解决办法是改变使用纯XML操作的配置文件的设置。我覆盖安装方法和更改文件本身。

The only solution I have found is to change the settings using plain XML manipulation of the config file. I override the Install method and make changes to the file itself.