Again, user config files C#

大兔子大兔子 提交于 2019-12-09 03:23:28

A standard way of using defaults in the app.config. For example, here's a default value per version I use to ensure the code copies user settings between upgrades:

<userSettings>
    <Software.Namespace.Properties.UserSettings>
      <setting name="RequiresUpgrade" serializeAs="String">
        <value>True</value>
      </setting>
    </Software.Namespace.Properties.UserSettings>
  </userSettings>
  <-- .... -->
  <userSettings>
    <Software.Namespace.Properties.UserSettings>
      <setting name="RequiresUpgrade" serializeAs="String">
        <value>True</value>
      </setting>
    </Software.Namespace.Properties.UserSettings>
  </userSettings>
</configuration>

You will need to add a .settings file to your project, or go to your project properties -> Settings and configure them from there.

The location of the user's own settings file is usually placed within their AppSettings folder in their profile. I'm not sure if this can be changed, but I seem to remember reading out it.

EDIT

There's some more information about it here: Application Settings Architecture

It also shows the following example if you want to keep it simple:

[UserScopedSetting()]
[DefaultSettingValue("white")]
public Color BackgroundColor
{
    get
    {
        return ((Color)this["BackgroundColor"]);
    }
    set
    {
        this["BackgroundColor"] = (Color)value;
    }
}

And this looks like it's very important to note when using this (quote):

For a Windows Forms-based application copied onto the local computer, app.exe.config will reside in the same directory as the base directory of the application's main executable file, and user.config will reside in the location specified by the Application.LocalUserAppDataPath property. For an application installed by means of ClickOnce, both of these files will reside in the ClickOnce Data Directory underneath %InstallRoot%\Documents and Settings\username\Local Settings.

The storage location of these files is slightly different if a user has enabled roaming profiles, which enables a user to define different Windows and application settings when he or she is using other computers within a domain. In that case, both ClickOnce applications and non-ClickOnce applications will have their app.exe.config and user.config files stored under %InstallRoot%\Documents and Settings\username\Application Data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!