是否有系统定义的文档目录环境变量?
我知道 Windows XP(以及 Vista 和 Windows 7)上的 %USERPROFILE%
系统定义的环境变量。是否有系统定义的环境变量指向“我的文档”目录的位置?在 XP 上默认为 %USERPROFILE%\My Documents
,在 Win 7 上默认为 %USERPROFILE%\Documents
。我只是想避免在 Powershell 脚本中测试操作系统版本(如果可以的话)。
I know about the %USERPROFILE%
system defined environment variable on Windows XP (and Vista and Windows 7). Is there a system defined environment variable pointing to the location of the "My Documents" directory? On XP by default it's %USERPROFILE%\My Documents
and on Win 7 it's %USERPROFILE%\Documents
. I just wanted to avoid having to test for the OS version in a Powershell script if I can avoid it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
对于 powershell,以下内容有效:
并避免魔术字符串
对于 .NET,以下内容成立(即不适用于所有 Windows 应用程序):
正如一个答案指出的那样,没有环境变量指向“我的文档”,但.NET 有
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
(C#)。我添加这个答案是因为在谷歌搜索 C#、环境变量和我的文档时出现这个问题,而 Justin 的答案不包含代码行:)
首选使用上述代码行在 .NET 中访问我的文档的方式:)
复制粘贴此行以供 C# 使用:
请注意,C# 在 MyDocuments 中需要大写 D。
For powershell the following works:
and avoiding magic strings
For .NET the following holds true (ie not applicable in all windows applications):
As one answer points out, there is no Environment Variable pointing to My Documents but there is
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
(C#) for .NET.I'm adding this answer since this question comes up when googling for C#, environment variables and my documents and Justin's answer does not contain the line of code :)
Using the above mentioned line of code is the preferred way of accessing my documents in .NET :)
Copy paste this row for C# usage:
Note that C# needs a capital D in MyDocuments.
在我的默认安装 XP 系统上,没有相应的环境变量。您可以在命令行中使用“set”命令(无参数)列出所有变量。所以可能你必须做一个测试。
如果您不想测试操作系统版本,您可以简单地检查“文档”是否存在,如果不存在,则尝试“我的文档”,反之亦然。然而这并不完美,因为 s/o 的 XP 计算机上可能有一个“Documents”文件夹。
顺便说一句:我的系统是德语,所以该文件夹名为“Dokumente”。您可能需要考虑到这一点。
该文件夹的路径存储在
Personal
下。不过,您需要注册表访问权限。来源:微软
On my default-installation XP system, there is no environment variable for that. You can list all variables with the "set" command ( no parameters ) in the command line. So probably you have to do a test.
If you don't want to test for the OS version, you can simply check whether "Documents" exists and if not then try "My Documents" or vice versa. This isn't perfect however, because s/o could have a "Documents" folder on his XP machine.
Btw: my system is German, so the folder is called "Dokumente". You might need to take that into account.
The path to that folder is stored in
under
Personal
. You need registry access, though.Source: Microsoft
没有内置的环境变量,但在 PowerShell 中您可以使用以下命令找到该位置:
您显然还可以使用以下命令创建环境变量:
There's no inbuilt enviornment variable, but in PowerShell you can find the location with:
You can also obviously create an environment variable with:
(只是为了重申前面的答案)没有为“我的文档”目录提供开箱即用(WinXP)的环境变量。
但是,您可以使用以下命令设置一个变量:
在 Windows 7 / 8.1 上测试:
或(一行)
这将为您提供一个
%mydocuments%
变量:(有人使用 XP/Vista 吗? ?如果是,可以测试一下并让我们知道它是否有效?)
(Just to reiterate the previous answers) There is no environment variable provided out-of-the-box (WinXP) for the "My Documents" directory.
However, you can set a variable, with the following command:
Tested on Windows 7 / 8.1:
or (one liner)
Which would then give you a
%mydocuments%
variable:(Does anyone use XP/Vista? If yes, can test this and let us know if it works?)
如果您键入:
在命令提示符中,您将获得系统上定义的所有环境变量的列表。
看看我的(Windows 7 Home Premium)上定义的那些,它们似乎都没有指向“我的文档”。
仅供参考:
SHGetSpecialFolderPath 函数可用于获取我的文档目录的路径。或者,可以在 .Net 下使用 Environment.GetFolderPath 方法
If you type:
In a command prompt you will get a list of all environment variables defined on your system.
Looking at the ones defined on mine (Windows 7 Home Premium) none of them appear to point towards My Documents.
FYI:
The SHGetSpecialFolderPath function can be used to get the path to the My Documents directory. Alternatively the Environment.GetFolderPath method can be used under .Net
正如你所看到的,不存在这样的变量。
as you can see there is no such a vairable.
除了基于注册表的答案之外,.NET 和 PowerShell,您还可以使用.NET 和 PowerShell microsoft.com/en-us/library/9x9e7edx(v=vs.84).aspx" rel="nofollow noreferrer">
WshSpecialFolders
来自 WSH。这是一个自包含命令/批处理脚本,演示了如何操作:它在 JScript 中发出 WSH 脚本,并使用它来获取作为参数提供的特殊文件夹标记的一个或多个路径。假设您将上述脚本保存为名为
specialf.cmd
的文件,获取当前用户文档目录路径的用法将是:这是测试所有特殊文件夹令牌的另一种用法:
您可以使用它来捕获像这样的环境变量:
In addition to answers based on registry, .NET and PowerShell, you could also use
WshSpecialFolders
from WSH. Here's a self-contained command/batch script demonstrating how:It emits a WSH script in JScript and uses it to get one or more paths for special folder tokens supplied as arguments. Assuming you save the above script as a file called
specialf.cmd
, the usage for getting path to current user's documents directory would be:Here's another usage testing all special folder tokens:
You could use this to capture into an environment variable like this:
一些混乱可能是由于 CSIDL/KNOWNFOLDERID 值与命令 shell 环境变量的可用性造成的。
Some confusion may be due to the availability of CSIDL/KNOWNFOLDERID values vs command shell environment variables.
对于 Windows 7 中的批处理文件(至少),Nick G 的解决方案需要稍作调整来设置用户定义的变量 UserDocuments :
请注意,唯一的区别是,
避免看到这条线,但要查看结果,请使用:
谢谢 Nick G。你的回答教会了我很多。我希望这对其他人有帮助。
For a batch file in Windows 7 (at least), Nick G's solution needs a slight tweak to set the user-defined variable UserDocuments :
Note the only differences are,
To avoid seeing the line, but to see the results, use :
Thanks Nick G. Your answer taught me a lot. I hope this helps someone else.
改进了 @NickGrealy 答案:
reg query
输出%a %b
字符串不正确在本例中,因此,使用
skip=2
选项跳过第一行,并使用tokens=2*
选项将注册表值传递给%b var:
或对于脚本文件:
但考虑到注册表值
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\!请勿使用此注册表项]
基于@阿蒂夫·阿齐兹 回答:
Improved @NickGrealy answer:
reg query
outputs%a %b
string is not correct in this caseSo, using the
skip=2
option to skip first lines and thetokens=2*
option to pass a registry value to the%b
var:or for script files:
But taking into account the registry value
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\!Do not use this registry key]
Based on @AtifAziz answer:
Windows 中设计时不存在文档环境变量。您必须创建一个定制的。请前往此处执行此操作。定义一个名为 MYDOCUMENTS 的环境变量来引用您需要引用的任何位置。此后,它应该是您通过 %MYDOCUMENTS% 引用的环境变量。
There does not exist by design a documents environment variable in windows. You have to create a customized one. Do this by going here. Define an environment variable called MYDOCUMENTS to reference whichever location you need referenced. Thereafter, it shall be an environment variable that you reference by %MYDOCUMENTS%.
已在 win XP、vista、8、8.1 和 10 中测试并运行!
Tested and worrking in win XP, vista, 8, 8.1 and 10!!
Windows 批处理文件 (.bat) 或 Windows 命令脚本 (.cmd)
Windows Batch File (.bat) or Windows Command Script (.cmd)
更新 Windows 批处理文件 (.bat) 或 Windows 命令脚本 (.cmd)
update Windows Batch File (.bat) or Windows Command Script (.cmd)
以下是运行 Windows 的所有用户的用户变量的完整列表。
代码的 reg 查询部分将找到该值并对其进行设置,以便可以立即使用它,并允许其他代码为所有用户永久设置它。
代码的广告注册部分将为所有用户启用它。 setx 使您无需注销并重新登录即可使用代码。
当前用户变量仅适用于安装此代码的人。
Here's the full list of user variables for all users running Windows.
The reg query portion of the code will find that value and set it so it can be used immediately and allows the other codes to set it permanently for all users.
The ad registry portion of the code will enable it for all users. The setx makes it so you don't have to log off and log back in to be able to use the codes.
The current user variables only applies to the person who installed this code.
实际上,
%USERPROFILE%\My Documents
应该可以在 Windows 7 中工作。这就是我使用的。Actually, the
%USERPROFILE%\My Documents
should work in Windows 7. It's what I use.