我需要我的 C# 设置文件是静态的(如在一个地方)
目前,我有通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法使用 Properties.Settings 功能执行此操作,因为
另一种方法是使用 System.IO API 来读取和写入您自己设计的配置文件。
You can't do this with the Properties.Settings feature, as the documenation says:
An alternate approach would be to use the System.IO API to read and write a configuration file you design yourself.
在 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.
您可以将设置放在应用程序配置文件的
标记中。请参阅 http://msdn.microsoft.com/en-us/library/ms229207。 aspx 有关标签的文档。
例如(xxxxx -> Visual Studio 项目名称,yyyyy-> 设置文件中的实际设置)。
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).
如果不扩展和大量修改 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