这可以正常工作,但是要记住的一件事是,如果您安装新版本的程序,它将“丢失”旧设置(因为这些设置特定于您的程序的特定版本)。(“版本”是指 AssemblyVersion)
幸运的是,您可以通过在 Main() 开始处或附近调用以下函数来处理此问题。为此,您需要添加一个名为 NeedSettingsUpgrade 的新布尔设置属性并将其默认为“true”:
/// <summary>Upgrades the application settings, if required.</summary>
private static void upgradeProgramSettingsIfNecessary()
{
// Application settings are stored in a subfolder named after the full #.#.#.# version
// number of the program. This means that when a new version of the program is installed,
// the old settings will not be available.
//
// Fortunately, there's a method called Upgrade() that you can call to upgrade the settings
// from the old to the new folder.
//
// We control when to do this by having a boolean setting called 'NeedSettingsUpgrade' which
// is defaulted to true. Therefore, the first time a new version of this program is run, it
// will have its default value of true.
//
// This will cause the code below to call "Upgrade()" which copies the old settings to the new.
// It then sets "NeedSettingsUpgrade" to false so the upgrade won't be done the next time.
if (Settings.Default.NeedSettingsUpgrade)
{
Settings.Default.Upgrade();
Settings.Default.NeedSettingsUpgrade = false;
}
}