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

房东的猫 提交于 2019-12-13 07:30:32

问题


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:


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.



来源:https://stackoverflow.com/questions/1591725/how-do-i-alter-a-net-application-user-settings-on-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!