问题
I want to be able to load some information programmatically into Properties.Settings.Default
before I publish it, but it doesn't persist. How do I overcome that?
I have (as a test):
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Setting1 = "abc";
Properties.Settings.Default.Save();
}
private void button2_Click(object sender, EventArgs e)
{
Text = Properties.Settings.Default.Setting1;
}
I clicked on button1, then published (with clickonce) and then run the published application and clicked on button2. The Text
was empty.
If don't publish the application, rather just close it and reopen it and click on button2 – I get "abc".
回答1:
See the following post http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C
回答2:
publishing won't click the button for you.
You need to detect the setting isn't initialised correctly (compare with an application setting perhaps) and then set and save it yourself.
Blank / doesn't exist / = somesettingToUpgrade
and a little routine to find a setting by name, set and save then you could put that in the button click handler as well as say FormLoad.
回答3:
It seems that the problem was that the scope was "user". But if the scope is "application" – it can't be changed programmatically.
So there seems not to be a way to do it. Persist from build programmatically, that is.
来源:https://stackoverflow.com/questions/10302772/how-to-persist-from-build-programmatically