windows 桌面路径的环境变量是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2000638/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:01:10  来源:igfitidea点击:

What's the environment variable for the path to the desktop?

windowspowershellvbscriptlocalizationdesktop

提问by Scott Langham

I'm writing a Windows batch file and want to copy something to the desktop. I think I can use this:

我正在编写一个 Windows 批处理文件,并希望将某些内容复制到桌面。我想我可以用这个:

%UserProfile%\Desktop\

%UserProfile%\桌面\

However, I'm thinking, that's probably only going to work on an English OS. Is there a way I can do this in a batch file that will work on any internationalized version?

但是,我在想,这可能只适用于英文操作系统。有没有办法在批处理文件中执行此操作,该文件适用于任何国际化版本?

UPDATE

更新

I tried the following batch file:

我尝试了以下批处理文件:

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%

And got this output:

并得到这个输出:

S:\>REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
    Desktop    REG_EXPAND_SZ    %USERPROFILE%\Desktop


S:\>FOR /F "usebackq tokens=3 skip=4" %i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folder
s" /v Desktop`) DO SET DESKTOPDIR=%i

S:\>FOR /F "usebackq delims=" %i in (`ECHO ECHO is on.`) DO SET DESKTOPDIR=%i

S:\>SET DESKTOPDIR=ECHO is on.

S:\>ECHO ECHO is on.
ECHO is on.

采纳答案by INS

I found that the best solution is to use a vbscript together with the batch file.

我发现最好的解决方案是将 vbscript 与批处理文件一起使用。

Here is the batch file:

这是批处理文件:

@ECHO OFF
FOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%

Here is findDesktop.vbs file:

这是 findDesktop.vbs 文件:

set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)

There may be other solutions but I personally find this one less hackish.

可能还有其他解决方案,但我个人认为这个解决方案不那么骇人听闻。

I tested this on an English PC and also a French PC - it seems to work (Windows XP).

我在一台英语 PC 和一台法语 PC 上对此进行了测试 - 它似乎可以工作(Windows XP)。

HTH,

哈,

Iulian ?erb?noiu

尤利安?erb?noiu

回答by Kb.

To be safe, you should use the proper APIs in Powershell (or VBScript)
Using PowerShell:

为安全起见,您应该使用 PowerShell 在 Powershell(或 VBScript)中
使用正确的 API :

[Environment]::GetFolderPath("Desktop")

Copy something using Powershell:

使用 Powershell 复制一些东西:

Copy-Item $home\*.txt ([Environment]::GetFolderPath("Desktop"))

Here is a VBScript-example to get the desktop path:

这是获取桌面路径的 VBScript 示例:

dim WSHShell, desktop, pathstring, objFSO
set objFSO=CreateObject("Scripting.FileSystemObject")
Set WSHshell = CreateObject("WScript.Shell")
desktop = WSHShell.SpecialFolders("Desktop")
pathstring = objFSO.GetAbsolutePathName(desktop)
WScript.Echo pathstring

回答by Saqib

KB's answer to use [Environment]::GetFolderPath("Desktop")is obviously the official Windows API for doing this.

KB 的使用答案[Environment]::GetFolderPath("Desktop")显然是用于执行此操作的官方 Windows API。

However, if you're working interactively at the prompt, or just want something that works on your machine, the tilda (~) character refers to the current user's home folder. So ~/desktopis the user's desktop folder.

但是,如果您在提示符下以交互方式工作,或者只是想要在您的机器上运行的东西,则 tilda (~) 字符指的是当前用户的主文件夹。因此~/desktop是用户的桌面文件夹。

回答by Vitim.us

At least on Windows XP, Vista and 7 you can use the "%UserProfile%\Desktop"safely.

至少在 Windows XP、Vista 和 7 上您可以"%UserProfile%\Desktop"安全地使用。

Windows XP en-US it will expand to "C:\Documents and Settings\YourName\Desktop"
Windows XP pt-BR it will expand to "C:\Documents and Settings\YourName\Desktop"
Windows 7 en-US it will expand to "C:\Users\YourName\Desktop"
Windows 7 pt-BR it will expand to "C:\Usuarios\YourName\Desktop"

Windows XP en-US 它将扩展到"C:\Documents and Settings\YourName\Desktop"
Windows XP pt-BR 它会扩展到"C:\Documents and Settings\YourName\Desktop"
Windows 7 en-US 它会扩展到"C:\Users\YourName\Desktop"
Windows 7 pt-BR 它会扩展到"C:\Usuarios\YourName\Desktop"

On XP you can't use this to others folders exept for Desktop My documentsturning to Meus Documentosand Local Settingsto Configuracoes locaisPersonaly I thinks this is a bad thing when projecting a OS.

在XP中,你不能用这个文件夹,别人对exept桌面 My documents转向Meus DocumentosLocal SettingsConfiguracoes locais本人来说我认为,这突出的OS时是一件坏事。

回答by Dave Webb

Not only would that not work for an International version of Windows, it would fail if the user had edited the Registry to make their Desktop folder reside somewhere else. You can query the Registry for the file location using the REGcommand:

这不仅不适用于国际版的 Windows,而且如果用户编辑注册表以将其桌面文件夹驻留在其他地方,它也会失败。您可以使用以下REG命令在注册表中查询文件位置:

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop

To get this into a variable use something like this:

要将其转换为变量,请使用以下内容:

FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%

回答by Dave Webb

you could also open a DOS command prompt and execute the setcommand.

您还可以打开 DOS 命令提示符并执行set命令。

This will give you an idea what environment variables are available on your system.

这将使您了解系统上可用的环境变量。

E.g. - since you where specifically asking for a non-english Windows - heres is an example of my own German Edition (Window7-64bit):

例如 - 因为你特别要求非英文 Windows - 这是我自己的德语版(Window7-64bit)的一个例子:

set > env.txt
type env.txt

ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\SOF\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=VMSOF
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\SOF
LOCALAPPDATA=C:\Users\SOF\AppData\Local
LOGONSERVER=\VMSOF
NUMBER_OF_PROCESSORS=2
OS=Windows_NT
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\CMake 2.8\bin;C:\Program Files (x86)\emacs-22.3\bin;C:\Program Files (x86)\GnuWin32\bin;
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=AMD64 Family 15 Model 67 Stepping 3, AuthenticAMD
PROCESSOR_LEVEL=15
PROCESSOR_REVISION=4303
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\SOF\AppData\Local\Temp
TMP=C:\Users\SOF\AppData\Local\Temp
USERDOMAIN=VMSOF
USERNAME=SOF
USERPROFILE=C:\Users\SOF
VBOX_INSTALL_PATH=C:\Program Files\Sun\VirtualBox\
VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\
windir=C:\Windows

回答by Osama Rajab

in windows 7 this returns the desktop path:

在 Windows 7 中,这将返回桌面路径:

FOR /F "usebackq tokens=3 " %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i 
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i 
ECHO %DESKTOPDIR% 

回答by MBu

I had a similar problem (and VBScript or PowerShell was not an option) and the code I found in this article did not work for me. I had problems with OS versions and language versions. After some experiments I've come to this solution:

我遇到了类似的问题(VBScript 或 PowerShell 不是一种选择),我在本文中找到的代码对我不起作用。我遇到了操作系统版本和语言版本的问题。经过一些实验,我得出了这个解决方案:

for /f "usebackq tokens=2,3*" %%A in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop"`) do if %%A==REG_EXPAND_SZ call :reparse set desktopdir=%%B
echo %desktopdir%
goto :EOF

:reparse
%*
goto :EOF

This code works for me in English and Polish versions of Windows 7 and Windows XP.

此代码适用于 Windows 7 和 Windows XP 的英语和波兰语版本。

The :reparse subroutine allows for delayed expansion of environment variables.

:reparse 子例程允许延迟扩展环境变量。

回答by fupsduck

This is not a solution but I hope it helps: This comes close except that when the KEY = %userprofile%\desktop the copy fails even though zdesktop=%userprofile%\desktop. I think because the embedded %userprofile% is not getting translated.

这不是一个解决方案,但我希望它有所帮助:这很接近,除了当 KEY = %userprofile%\desktop 复制失败时,即使 zdesktop=%userprofile%\desktop。我认为是因为嵌入的 %userprofile% 没有被翻译。

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop>z.out
for /f "tokens=3 skip=4" %%t in (z.out) do set zdesktop=%%t
copy myicon %zdesktop%
set zdesktop=
del z.out

So it sucessfully parses out the REG key but if the key contains an embedded %var% it doesn't get translated during the copy command.

因此,它成功解析出 REG 键,但如果该键包含嵌入的 %var%,则不会在复制命令期间进行翻译。

回答by Improv

While I realize this is a bit of an older post, I thought this might help people in a similar situation. I made a quick one line VBScript to pull info for whatever special folder you would like (no error checking though) and it works like this:

虽然我意识到这是一篇较旧的帖子,但我认为这可能会帮助处于类似情况的人。我做了一个快速的一行 VBScript 来为你想要的任何特殊文件夹提取信息(虽然没有错误检查),它的工作原理是这样的:

Create a file "GetShellFolder.vbs" with the following line:

使用以下行创建一个文件“GetShellFolder.vbs”:

WScript.Echo WScript.CreateObject("WScript.Shell").SpecialFolders(WScript.Arguments(0))

I always make sure to copy cscript.exe (32-bit version) to the same folder as the batch file I am running this from, I will assume you are doing the same (I have had situations where users have somehow removed C:\Windows\system32 from their path, or managed to get rid of cscript.exe, or it's infected or otherwise doesn't work).

我总是确保将 cscript.exe(32 位版本)复制到与我运行它的批处理文件相同的文件夹中,我假设您正在做同样的事情(我遇到过用户以某种方式删除了 C:\ Windows\system32 从他们的路径,或者设法摆脱 cscript.exe,或者它被感染或以其他方式不起作用)。

Now copy the file to be copied to the same folder and create a batch file in there with the following lines:

现在将要复制的文件复制到同一文件夹并在其中创建一个批处理文件,其中包含以下几行:

for /f "delims=" %%i in ('^""%~dp0cscript.exe" "%~dp0GetShellFolder.vbs" "Desktop" //nologo^"') DO SET SHELLDIR=%%i
copy /y "%~dp0<file_to_copy>" "%SHELLDIR%\<file_to_copy>"

In the above code you can replace "Desktop" with any valid special folder (Favorites, StartMenu, etc. - the full official list is at https://msdn.microsoft.com/en-us/library/0ea7b5xe%28v=vs.84%29.aspx) and of course <file_to_copy>with the actual file you want placed there. This saves you from trying to access the registry (which you can't do as a limited user anyway) and should be simple enough to adapt to multiple applications.

在上面的代码中,您可以用任何有效的特殊文件夹(收藏夹、开始菜单等)替换“桌面” - 完整的官方列表位于https://msdn.microsoft.com/en-us/library/0ea7b5xe%28v=vs .84%29.aspx),当然<file_to_copy>还有你想放在那里的实际文件。这使您免于尝试访问注册表(无论如何您不能以受限用户身份访问)并且应该足够简单以适应多个应用程序。

Oh and for those that don't know the "%~dp0"is just the directory from which the script is being called. It works for UNC paths as well which makes the batch file using it extremely portable. That specifically ends in a trailing "\" though so it can look a little odd at first glance.

哦,对于那些不知道这"%~dp0"只是调用脚本的目录的人。它也适用于 UNC 路径,这使得使用它的批处理文件非常便携。这特别以尾随的“\”结尾,所以乍一看可能有点奇怪。