How do I alter a .NET application/user settings on

2019-08-29 19:58发布

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

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.

1条回答
等我变得足够好
2楼-- · 2019-08-29 20:28

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

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.

查看更多
登录 后发表回答