這將工作正常,但有一點要記住的是,如果你安裝一個新版本的程序,它會「丟失「舊設置(因爲這些設置是特定於您程序的特定版本的)。 (通過「版本」我的意思是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;
}
}
如何試用? – ulrichb
您是否嘗試過使用此功能,然後在重新啓動應用程序後檢查值? [閱讀](http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx) – V4Vendetta
不錯,我仍在開發。我必須決定是採取這種方法還是使用其他技術可能將其保存在配置文件中 – Kiran