展开“我的文档”的环境变量
我知道我可以读取这样的环境变量:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
但是,如果我可以做这样的事情,那对我来说真的很有帮助:
Environment.ExpandEnvironmentVariables(@"%MyDocuments%\Foo");
Is there anenvironmentvariables that equals SpecialFolder.MyDocuments?
我也尝试做这样的事情,但这并没有达到预期的结果:
Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\My Documents\Foo");
这样我最终得到了类似 @"C:\Users\
@"\\someservername\users$\
。
编辑:我的目标不是对环境变量或之后的部分进行硬编码。
还有其他建议吗?
I know I can read environment variables like this:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
However, it would be really helpful to me if I could do something like this:
Environment.ExpandEnvironmentVariables(@"%MyDocuments%\Foo");
Is there an environement variable that equals SpecialFolder.MyDocuments?
I also tried to do something like this, but this doesn't lead to the expected result:
Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\My Documents\Foo");
This way I ended up with something like @"C:\Users\<MyUser>\My Documents\Foo"
but I what I need is @"\\someservername\users$\<MyUser>\My Documents\Foo"
.
EDIT: My Goal is NOT to hardcode either environment variable nor the part after that.
Any other suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
否,
MyDocuments
特殊文件夹没有环境变量(对于SpecialFolder
枚举的大多数成员也是如此)。查看此代码段,它可能正是您的情况寻找。
它允许您执行类似的操作:
注意:SpecialFolder.ExpandVariables 是上面代码片段中引入的辅助类的静态方法。
No there is no environment variable for the
MyDocuments
special folder (the same is true for most members of theSpecialFolder
enumeration).Check out this snippet, it might be exactly what you are searching for.
It allows you to do something like that:
Note: SpecialFolder.ExpandVariables is a static method of a helper class introduced in the above snippet.
简短回答:否。
详细回答:
还是不行。您可以在命令提示符中输入“set”来查看当前的所有环境变量。我在我的个人资料中找不到任何文档文件夹(在 WinXP 和 Win7 上尝试过)。
另外,扩展
"%USERPROFILE%\My Documents"
是不正确的,因为用户的文档文件夹可能位于其他任何地方(例如,在我的家用电脑上,我总是将其更改为D:\Documents< /代码>)。
如果您确实需要使用环境变量,一种解决方案可能是自己设置变量:
另一种解决方案可能是在路径中使用“假”环境变量并自行扩展它,例如:
Short answer: No.
Long answer:
Still no. You can type
"set"
into a Command Prompt to see all you current environment variables. I couldn't find any for my documents folder on my profile (tried on WinXP and Win7).Also, expanding
"%USERPROFILE%\My Documents"
would be incorrect since the user's documents folder could be anywhere else (e.g., on my home PC I always change mine toD:\Documents
).If you really need to use environment variables, one solution might be to set the variable yourself:
Another solution might be to use a "fake" environment variable in the path and expand it yourself, something like:
你到底想做什么?有什么理由不能只使用
路径.组合
?What exactly are you trying to do? Is there any reason why you can't just use
Path.Combine
?我不确定是否有好的方法可以做到这一点,但与其尝试进行环境扩展来获取路径,为什么不使用 Path.Combine API 呢?
I'm not sure if there is a good way to do this but instead of trying to do environment expansion to get the path why not use the
Path.Combine
API instead?您可以使用
Environment.GetEnvironmentVariable
方法扩展环境变量。鉴于您的评论,我建议将您的路径分成 2 个单独的配置设置,以便更轻松地扩展它:You can expand environment variables using then
Environment.GetEnvironmentVariable
method. Given your comment, I would suggest breaking your path up into 2 separate config settings to make expanding it easier:不,它不存在。最简单的检查方法是从命令行运行“set”并查看自己。
No it does not exist. The easiest way to check is to run "set" from command line and see yourself.