同样,用户配置文件C# [英] Again, user config files C#

查看:90
本文介绍了同样,用户配置文件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的配置文件(用户),需要转到正确的位置并具有一些默认值。

I have a large config file (user) that i needed to go to the right location and have some default values.

由于我有安装程序类,因此我在其中的配置文件中添加了一些参数设置,但是在安装程序文件夹中创建了配置文件。

Since i have a installer class, i added some parameter setting to the config file in it, but created the config files in the installers folder.

什么是确保仅写入这些默认参数的最佳方法是什么一次并且在正确的位置?

What is the best way to ensure these default parameters will be written only once, and in the right location?

推荐答案

在app.config中使用默认值的标准方法。例如,这是我用来确保代码在两次升级之间复制用户设置的每个版本的默认值:

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>

您将需要在项目中添加.settings文件,或转到项目属性->设置并从那里进行配置。

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

用户自己的设置文件的位置通常位于配置文件中的AppSettings文件夹中。我不确定是否可以更改,但我似乎还记得读出来。

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

EDIT

此处有更多信息:应用程序设置体系结构

如果您想保持简单,还显示了以下示例:

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):


对于基于Windows窗体的应用程序
复制到本地计算机上,
应用程序。 exe.config将与
应用程序的主要可执行文件的基本目录位于同一
目录中,
和user.config 将位于由
指定的位置
Application.LocalUserAppDataPath
属性
。对于通过ClickOnce安装
的应用程序,这两个
文件都将驻留在
%InstallRoot%\Documents和
数据目录中>设置>用户名>本地设置。

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.

这些文件的存储位置为
,如果用户有$,则略有不同 b $ b启用了漫游配置文件,其中
使用户可以在域中使用
的其他计算机
定义不同的
Windows和应用程序设置。在这种情况下,
ClickOnce应用程序和
非ClickOnce应用程序都将
的app.exe.config和user.config
文件存储在
%InstallRoot%下\文档和
设置\用户名\应用程序数据

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.

这篇关于同样,用户配置文件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆