我需要我的 C# 设置文件是静态的(如在一个地方)

发布于 2024-11-26 18:41:55 字数 184 浏览 2 评论 0原文

目前,我有通过 Properties.Settings.Default 访问的默认 Settings.settings 文件。

但此配置保存在我用户的 appdata 文件夹中。如何才能只有一个配置文件与通用的 exe 保存在同一目录中,并且可以在运行时更改?

Currently I have the the default Settings.settings file that I access through Properties.Settings.Default.

But this config is saved in my user's appdata folder. How can I get there to be only one config file kept in the same dir as the exe that is universal AND can be changed at runtime?

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

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

发布评论

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

评论(4

公布 2024-12-03 18:41:55

您无法使用 Properties.Settings 功能执行此操作,因为

应用程序范围设置是只读的,并且只能在
设计时或通过更改 .exe.config 文件
在应用程序会话之间。但是,用户范围设置可以是
在运行时编写,就像更改任何属性值一样。这
新值在应用程序会话期间持续存在。你
可以通过以下方式在应用程序会话之间保留对用户设置的更改
调用Settings.Save方法。这些设置保存在
User.config 文件。

另一种方法是使用 System.IO API 来读取和写入您自己设计的配置文件。

You can't do this with the Properties.Settings feature, as the documenation says:

Application-scope settings are read only, and can only be changed at
design time or by altering the .exe.config file in
between application sessions. User-scope settings, however, can be
written at run time, just as you would change any property value. The
new value persists for the duration of the application session. You
can persist changes to user settings between application sessions by
calling the Settings.Save method. These settings are saved in the
User.config file.

An alternate approach would be to use the System.IO API to read and write a configuration file you design yourself.

笔芯 2024-12-03 18:41:55

在 Vista/7 上你会遇到 UAC 问题。
请将重要的应用程序数据文件(例如“设置”)保存在隐藏且可访问的目录 - AppData 中。
UAC 允许访问此目录及其子目录。

如果您想在所有用户之间共享该文件,您可能需要使用“CommonAppData”目录,但您需要管理员权限才能在那里写入。

You're gonna have problems with UAC on Vista/7.
Please, do keep vital application data files such as the "Settings" in a hidden and accessible directory - AppData.
UAC allows access to this directory and it's subdirectories.

If you want to share the file among all the users, you might want to use the "CommonAppData" directory, but you need administrator rights to write there.

最单纯的乌龟 2024-12-03 18:41:55

您可以将设置放在应用程序配置文件的 标记中。

请参阅 http://msdn.microsoft.com/en-us/library/ms229207。 aspx 有关标签的文档。

例如(xxxxx -> Visual Studio 项目名称,yyyyy-> 设置文件中的实际设置)。

  <applicationSettings>
    <xxxxxx.Properties.Settings>
      <setting name="yyyyyy"
        serializeAs="String">
        <value>some_value</value>
      </setting>
    </ReprintProcess.Properties.Settings>
  </applicationSettings>

You can place the settings in the application configuration file in the <applicationSettings> tag.

See http://msdn.microsoft.com/en-us/library/ms229207.aspx for documentation on the tag.

Eg (xxxxx -> the Visual Studio project name, yyyyy-> actual setting in the Settings file).

  <applicationSettings>
    <xxxxxx.Properties.Settings>
      <setting name="yyyyyy"
        serializeAs="String">
        <value>some_value</value>
      </setting>
    </ReprintProcess.Properties.Settings>
  </applicationSettings>
苍暮颜 2024-12-03 18:41:55

如果不扩展和大量修改 Properties.Settings 类,就无法实现它。
因此,我建议您编写自己的设置管理包装器或替代库,例如 Settings4Net

You cannot achieve it without extending and heavily modifying Properties.Settings class.
Therefore I would recommend either writing your own Settings Management wrapper or alternative libraries such as Settings4Net

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