Environment.SpecialFolderPath 与 .NET 设置(以及解析文本框绑定中的环境变量)
我是一名 C# 菜鸟,我一直在寻找有关 C# 应用程序开发最佳实践的信息。
我想要一个应用程序设置来定义应用程序将在何处写入其日志文件和日志文件。报告输出。我希望将其默认为 %USERPROFILE%\Documents\
,因为这些文件是为用户在闲暇时实际查看和处理而创建的。但是,如果用户希望将文件写入其他位置,我想保留他们的首选项。
到目前为止,我的研究表明:
- 直接使用环境变量通常不是一个好主意。相反,建议使用 Environment.GetFolderPath方法和Environment.SpecialFolders枚举来访问Windows中的特殊位置,例如用户配置文件。
- 但是,建议使用 用户范围的 .NET 应用程序设置,将设置从一个会话保留到另一个会话。
有什么好的方法可以协调最佳实践 1 和最佳实践 2 吗?实际上是否可以在不引用 %USERPROFILE% 环境变量的情况下以兼容的方式将此默认位置存储在应用程序设置中?
一个相关问题: 假设我保留 %USERPROFILE%\Documents\
作为设置的默认值。我希望将设置的值绑定到主应用程序表单上的文本框(使用“浏览”按钮选择新路径)。如何将设置绑定到文本框,以便将 %USERPROFILE% 解析为传统文件系统路径(例如 C:\Users\
)用于向用户显示?
I'm a C# noob, and I've been looking for info on best practices for C# application development.
I want to have an application setting that defines where the application will write its log files & reporting output. I want this to default to %USERPROFILE%\Documents\<Vendor>\<Tool>
, since these files are created for the user to actually look at and dispose of at their leisure. But, if the user wants the files to be written to some other location, I want to persist their preferences.
My research so far indicates that:
- It's not generally a good idea to use environment variables directly. Instead, it's recommended to use the Environment.GetFolderPath method and the Environment.SpecialFolders enum to get access to special locations in Windows like the user profile.
- But, it's recommended to use user-scope .NET Application Settings to persist settings from one session to another.
Is there any good way to reconcile best practice 1 and best practice 2? Is it actually possible to store this default location in the Application Settings in a compatible way without referencing the %USERPROFILE% environment variable?
A related question:
Assume I keep %USERPROFILE%\Documents\<vendor>\<tool>
as the default value for the Setting. I want the Setting's value to be bound to a textbox on the main application form (with a Browse button to select a new path). How can I bind the Setting to a textbox in such a way that %USERPROFILE% is resolved to a conventional filesystem path (eg C:\Users\<username>\Documents\<vendor>\<tool>
) for purposes of display to the user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为使用环境变量存储路径实际上是将特殊文件夹作为字符串引用的最佳方式,并且仍然使用户可以轻松编辑路径(如果他/她决定这样做)。
通过调用 可以将环境变量解析为 .NET 中的简单字符串Environment.ExpandEnvironmentVariables 方法。您可以在文本框中显示路径之前使用此方法,从而使用户可以轻松编辑该路径。我想,一旦用户自定义了路径,您就可以将其存储为绝对路径。
I think that storing paths with environment variables is actually the best way to reference a special folder as a string and still make the path easily editable by the user, should s/he decide to do so.
Environment variables can be resolved to a simple string in .NET by calling the Environment.ExpandEnvironmentVariables method. You can use this method just before presenting the path in your TextBox and thus make it easily editable for the user. Once the user customizes the path, you can store it as an absolute path from there on, I suppose.