问题
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