再次,用户配置文件 C#

发布于 2024-08-26 04:10:10 字数 139 浏览 4 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两仪 2024-09-02 04:10:10

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

<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 文件添加到您的项目中,或者转到您的项目属性 -> 。设置并从那里配置它们。

用户自己的设置文件的位置通常放置在其个人资料的 AppSettings 文件夹中。我不确定这是否可以改变,但我似乎记得读过它。

编辑

这里有一些关于它的更多信息:应用程序设置架构

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

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

这看起来在使用此(引用)时需要注意非常重要:

对于基于 Windows 窗体的应用程序
复制到本地计算机上,
app.exe.config 将驻留在同一目录中
目录作为基本目录
应用程序的主要可执行文件,
和 user.config 将驻留在
指定的位置
应用程序.LocalUserAppDataPath
财产。对于已安装的应用程序
通过 ClickOnce,这两种方法
文件将驻留在 ClickOnce
下面的数据目录
%InstallRoot%\文档和
设置\用户名\本地设置

这些文件的存储位置是
稍有不同如果用户有
启用漫游配置文件

使用户能够定义不同的
Windows 和应用程序设置
他或她正在使用其他计算机
在一个域内。在这种情况下,双方
ClickOnce 应用程序和
非 ClickOnce 应用程序将有
他们的 app.exe.config 和 user.config
文件存储在
%InstallRoot%\文档和
设置\用户名\应用程序数据

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文