繁体   English   中英

在C#中使用设置类

[英]Using Settings Class in C#

我正在使用Properties.Settings类来保存应用程序设置。 我想知道,一旦在客户端系统中部署,设置是否会在应用程序重新启动和系统重新启动之间保存。

考虑这种情况:

部署应用程序后,用户将通过UI将手机号码保存为

电话:1xxxx-45678

现在,我将电话号码另存为

 Properties.Settings.Default.ClientPhone = this.PhoneText.Text;
 Properties.Settings.Default.Save();

我知道吗,电话号码会在app.restarts和reboots中保存在应用程序中?

这是有关应用程序和用户设置的区别。 应用程序设置为只读。 用户设置将永久存储在每个用户的基础上。 您的代码正是更改和保存用户设置所需要的

请注意:由于它们被称为“用户设置”,它们将为机器上的每个用户单独存储! 您不能使用默认的.NET设置机制创建对所有用户都相同的可变设置。

不要重新发明轮子! 使用.NET设置机制-您在示例中所做的就是正确的:-)

这可以正常工作,但是要记住的一件事是,如果您安装该程序的新版本,它将“丢失”旧设置(因为这些设置特定于您程序的特定版本)。 (“版本”是指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;
    }
}

一个快速的谷歌应该已经为你做到了。

是的,根据msdn的说明,它们将: .NET allows you to create and access values (settings) that are persisted between application execution sessions.保留的.NET allows you to create and access values (settings) that are persisted between application execution sessions.

http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM