c# – 应用程序更新安装后,应用程序将丢失所有设置

这是仅针对某些用户的问题.每当我在Marketplace中发布我的应用程序的新版本时,我都会收到来自用户的电子邮件,说明应用程序中的所有设置都将丢失.

我自己无法重现这个,我没有可以擦除IsolatedStorage的代码.

如果那里的任何人都有可能导致这种情况的线索,那就太好了.

最佳答案
更新:不确定以下是否适用于WP7应用程序 – 我会留在这里以防万一.我只针对普通应用尝试了这个.

您需要“升级”旧的设置文件.

您还需要知道何时需要执行此操作(即仅在安装新版本时).

要知道何时需要升级设置,请在设置中添加一个名为(例如)NeedSettingsUpgrade的布尔值,并将其默认为true.

然后在Main()开头附近的某处调用以下函数:

/// <summary>Upgrades the application settings, if required.</summary>
private static void upgradeApplicationSettingsIfNecessary()
{
    // 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;
    }
}

注意:您当然需要在程序退出之前调用Settings.Default.Save(),否则设置更改将不会保留.

转载注明原文:c# – 应用程序更新安装后,应用程序将丢失所有设置 - 代码日志