.Net 使用特殊文件夹表示法
我希望能够做这样的事情:
IO.Directory.Exists("%USERPROFILE%")
原因是我想指定我的应用程序将使用的目录之一,作为配置文件中的纯文本。在某些情况下,我希望它嵌套在用户配置文件下,在这种情况下,配置文件将显示如下内容:
...
LocalDbDirectory = %USERPROFILE%\Application Data\My Toolkit\
...
或者我可能希望它位于网络位置,在这种情况下,它会显示如下内容:
...
LocalDbDirectory = N:\Common\My Toolkit Databases\
...
所以我需要能够使用 IO.Directory.Exists(...) 或等效方法解释简写符号。
有什么想法吗?
I want to be able to do something like this:
IO.Directory.Exists("%USERPROFILE%")
The reason being that I want to specify one of the directories which my application will use, as plain text in a config file. In some cases I will want it to be nested under the user profile, in which case the config file would read something like:
...
LocalDbDirectory = %USERPROFILE%\Application Data\My Toolkit\
...
Or I might want it to be in a network location, in which case it would read something like:
...
LocalDbDirectory = N:\Common\My Toolkit Databases\
...
So I need to be able to interpret the shorthand notation with methods such as IO.Directory.Exists(...) or equivalent.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要通过
Environment.ExpandEnvironmentVariables(path)
运行它们; 其中路径为@"%USERPROFILE%\Application Data\My Toolkit\"
(对于不包含%%
的路径执行此操作没有任何害处格式化令牌)You need to run them through
Environment.ExpandEnvironmentVariables(path)
; where path is@"%USERPROFILE%\Application Data\My Toolkit\"
(There is no harm in doing this for paths that do not contain%%
formatted tokens)如果简写是有效的环境变量,您可以解析它们的值:
从 .NET 4 开始,特殊文件夹支持包括用户配置文件:
If the short-hands are valid environment variables, you can resolve their value:
As of .NET 4, special folder support includes user profile: