windows 文档目录是否有系统定义的环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3492920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is There A System Defined Environment Variable For Documents Directory?
提问by Onorio Catenacci
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.
我知道%USERPROFILE%
Windows XP(以及 Vista 和 Windows 7)上系统定义的环境变量。是否有系统定义的环境变量指向“我的文档”目录的位置?默认情况下,在 XP 上它是%USERPROFILE%\My Documents
,在 Win 7 上它是%USERPROFILE%\Documents
. 如果可以避免的话,我只是想避免在 Powershell 脚本中测试操作系统版本。
采纳答案by msteiger
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.
在我默认安装的 XP 系统上,没有环境变量。您可以在命令行中使用“set”命令(无参数)列出所有变量。所以可能你必须做一个测试。
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.
如果您不想测试操作系统版本,您可以简单地检查“文档”是否存在,如果不存在,则尝试“我的文档”,反之亦然。然而,这并不完美,因为 s/o 在他的 XP 机器上可能有一个“文档”文件夹。
Btw: my system is German, so the folder is called "Dokumente". You might need to take that into account.
顺便说一句:我的系统是德语,所以文件夹被称为“Dokumente”。您可能需要考虑到这一点。
The path to that folder is stored in
该文件夹的路径存储在
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
under Personal
. You need registry access, though.
下Personal
。但是,您需要注册表访问权限。
Source: Microsoft
资料来源:微软
回答by flindeberg
For powershell the following works:
对于powershell,以下工作:
[environment]::getfolderpath("mydocuments")
and avoiding magic strings
并避免魔法字符串
[Environment]::GetFolderPath([Environment+SpecialFolder]::MyDocuments)
For .NET the following holds true (ie not applicable in allwindows applications):
对于 .NET,以下适用(即不适用于所有Windows 应用程序):
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.
正如一个答案指出的那样,没有指向我的文档的环境变量,但有Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
(C#) 用于 .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 theline of code :)
因为谷歌搜索时C#,环境变量和我的文档和贾斯汀的回答不包含此问题来了我加入这个答案的代码行:)
Using the above mentioned line of code is the preferred way of accessing my documents in .NET :)
使用上面提到的代码行是在 .NET 中访问我的文档的首选方式:)
Copy paste this row for C# usage:
复制粘贴此行以供 C# 使用:
var directoryNameOfMyDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Note that C# needs a capital D in MyDocuments.
请注意,C# 在 MyDocuments 中需要大写 D。
回答by Hipponax43
There's no inbuilt enviornment variable, but in PowerShell you can find the location with:
没有内置的环境变量,但在 PowerShell 中,您可以使用以下命令找到该位置:
[Environment]::GetFolderPath("mydocuments")
You can also obviously create an environment variable with:
您显然还可以创建一个环境变量:
$env:DOCUMENTS = [Environment]::GetFolderPath("mydocuments")
回答by Nick Grealy
(Just to reiterate the previous answers) There is no environment variable provided out-of-the-box (WinXP) for the "My Documents" directory.
(只是重申以前的答案)没有为“我的文档”目录提供开箱即用 (WinXP) 的环境变量。
However, you can set a variable, with the following command:
但是,您可以使用以下命令设置变量:
Tested on Windows 7 / 8.1:
在 Windows 7 / 8.1 上测试:
for /f "tokens=3* delims= " %a ^
in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') ^
do (set mydocuments=%a %b)
or (one liner)
或(一个班轮)
for /f "tokens=3* delims= " %a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set mydocuments=%a %b)
Which would then give you a %mydocuments%
variable:
然后会给你一个%mydocuments%
变量:
C:\>echo mydocuments="%mydocuments%"
mydocuments="C:\pathto\My Documents"
(Does anyone use XP/Vista? If yes, can test this and let us know if it works?)
(有人使用 XP/Vista 吗?如果是,可以测试一下,让我们知道它是否有效?)
回答by Justin
If you type:
如果您键入:
set
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.
看看我定义的那些(Windows 7 Home Premium),它们似乎都没有指向我的文档。
FYI:
供参考:
The SHGetSpecialFolderPathfunction can be used to get the path to the My Documents directory. Alternatively the Environment.GetFolderPathmethod can be used under .Net
该SHGetSpecialFolderPath函数可用于获取路径My Documents目录。或者,可以在 .Net 下使用Environment.GetFolderPath方法
回答by Moisei
C:\Documents and Settings\mrabinovitch>set | grep -i document
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\myuser\Application Data
HOMEPATH=\Documents and Settings\myuser
USERPROFILE=C:\Documents and Settings\myuser
as you can see there is no such a vairable.
如您所见,没有这样的变量。
回答by Atif Aziz
In addition to answers based on registry, .NETand PowerShell, you could also use WshSpecialFolders
from WSH. Here's a self-containedcommand/batch script demonstrating how:
除了基于注册表、.NET和PowerShell 的答案外,您还可以使用WshSpecialFolders
来自WSH。这是一个自包含的命令/批处理脚本,演示了如何:
@echo off
call :script > "%temp%\%~n0.js" && cscript //nologo "%temp%\%~n0.js" %*
goto :EOF
:script
echo var specialFolders = WScript.CreateObject('WScript.Shell').SpecialFolders;
echo if (WScript.Arguments.length === 0) {
echo for (var e = new Enumerator(specialFolders); !e.atEnd(); e.moveNext()) {
echo WScript.Echo(e.item());
echo }
echo } else {
echo for (var e = new Enumerator(WScript.Arguments); !e.atEnd(); e.moveNext()) {
echo WScript.Echo(specialFolders(e.item()));
echo }
echo }
goto :EOF
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:
它在 JScript 中发出一个 WSH 脚本,并使用它来获取作为参数提供的特殊文件夹标记的一个或多个路径。假设您将上述脚本保存为名为 的文件specialf.cmd
,则获取当前用户文档目录路径的用法为:
specialf MyDocuments
Here's another usage testing all special folder tokens:
这是测试所有特殊文件夹令牌的另一种用法:
specialf ^
AllUsersDesktop ^
AllUsersStartMenu ^
AllUsersPrograms ^
AllUsersStartup ^
Desktop ^
Favorites ^
Fonts ^
MyDocuments ^
NetHood ^
PrintHood ^
Programs ^
Recent ^
SendTo ^
StartMenu ^
Startup ^
Templates
You could use this to capture into an environment variable like this:
您可以使用它来捕获到这样的环境变量中:
for /f "delims=/" %p in ('specialf MyDocuments') do @set MYDOCS=%p
回答by Rocky Scott
For a batch file in Windows 7 (at least), Nick G's solution needs a slight tweak to set the user-defined variable UserDocuments :
对于 Windows 7 中的批处理文件(至少),Nick G 的解决方案需要稍微调整以设置用户定义的变量 UserDocuments :
FOR /F "tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set UserDocuments=%%a)
Note the only differences are,
请注意,唯一的区别是,
- Use only one space character for delims
- %%a instead of %a
- 仅对 delim 使用一个空格字符
- %%a 而不是 %a
To avoid seeing the line, but to see the results, use :
为避免看到该行,但要查看结果,请使用:
@FOR /F "tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @(Set UserDocuments=%%a)
@Echo ~~~~~~~~ UserDocuments=%UserDocuments%
Thanks Nick G. Your answer taught me a lot. I hope this helps someone else.
谢谢尼克 G。你的回答教会了我很多。我希望这对其他人有帮助。
回答by John Chacho
Some confusion may be due to the availability of CSIDL/KNOWNFOLDERID values vs command shell environment variables.
一些混淆可能是由于 CSIDL/KNOWNFOLDERID 值与命令外壳环境变量的可用性。
回答by Dmitry Sokolov
Improved @NickGrealy answer:
改进的@NickGrealy 答案:
reg query
outputs
reg query
产出
empty_line reg_key_path name type value
- there can be an arbitrary amount of 'space chars' between words in a registry value, and the
%a %b
string is not correct in this case
- 注册表值中的单词之间可以有任意数量的“空格字符”,
%a %b
在这种情况下字符串不正确
So, using the skip=2
option to skip first lines and the tokens=2*
option to pass a registry value to the %b
var:
因此,使用skip=2
跳过第一行的tokens=2*
选项和将注册表值传递给%b
var的选项:
for /f "skip=2 tokens=2*" %A in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "UserDocs=%B"
or for script files:
或脚本文件:
for /f "skip=2 tokens=2*" %%A in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do set "UserDocs=%%B"
But taking into account the registry value [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\!Do not use this registry key]
但考虑到注册表值 [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\!Do not use this registry key]
Based on @AtifAziz answer:
基于@AtifAziz 的回答:
for /f "tokens=*" %A in ('echo WScript.Echo^(^(new ActiveXObject^("WScript.Shell"^)^).SpecialFolders^("MyDocuments"^)^)^>%TEMP%\getdoc.js ^& cscript /nologo %TEMP%\getdoc.js ^& del /q %TEMP%\getdoc.js') do @set "UserDocs=%A"