是否有任何环境变量代表 Windows 中的“C:\Documents and Settings”文件夹或 C:\Users 文件夹?

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

Is there any environment variable that represents "C:\Documents and Settings" folder or C:\Users folder in Windows?

windowsenvironment-variablesjavascript

提问by svv

Is there any environment variable or Other format that the profile path is represented in Windows? I want to query in such a way that I should get the value "C:\Documents and Settings (if windows XP or 2k3) or C:\users (If vista or windows 7).

是否有任何环境变量或其他格式在 Windows 中表示配置文件路径?我想以这样的方式查询,我应该得到值“C:\Documents and Settings(如果是 windows XP 或 2k3)或 C:\users(如果是 vista 或 windows 7)。

I dont want the current user name appended to the string, which I can get thru %USERPROFILE% variable.

我不希望将当前用户名附加到字符串中,我可以通过 %USERPROFILE% 变量获取它。

回答by tenfour

It doesn't exist. Instead, try %USERPROFILE%\..

它不存在。相反,尝试%USERPROFILE%\..

Warning: as @Mark suggests, this is not reliable because the user profile directory may really be any arbitrary location.

警告:正如@Mark 所暗示的那样,这是不可靠的,因为用户配置文件目录实际上可能是任意位置。

回答by martona

On Vista+ you can use FOLDERID_UserProfiles to get C:\Users (or whatever it may be in localized versions, etc). On XP and earlier you'll pretty much have to go the CSIDL_COMMON_DESKTOPDIRECTORY route that will give you "C:\Documents and Settings\All Users\Desktop" and work your way back from there.

在 Vista+ 上,您可以使用 FOLDERID_UserProfiles 来获取 C:\Users(或本地化版本中的任何内容等)。在 XP 及更早版本上,您几乎必须走 CSIDL_COMMON_DESKTOPDIRECTORY 路线,该路线将为您提供“C:\Documents and Settings\All Users\Desktop”,然后从那里返回。

I think this settles it for Vista. For XP the solution is not perfect, but at least it won't depend on the current user's profile path. "All Users" will always exist, and I can't think of a reason for it to be in a place other than the default.

我认为这解决了 Vista 的问题。对于 XP,该解决方案并不完美,但至少它不会依赖于当前用户的配置文件路径。“所有用户”将永远存在,我想不出将它放在默认位置以外的其他地方的原因。

回答by Sm1th

Yeah there actually is a way to get it to work:

是的,实际上有一种方法可以让它工作:

%USERPROFILE%\..

回答by dko

To the best of my knowledge no but you can do a last instance of '/' to find the parent directory of %USERPROFILE%

据我所知,否,但您可以执行最后一个“/”实例来查找 %USERPROFILE% 的父目录

回答by Lizz

I derived the batch and VBS methods (below), since I couldn't find an equivalent batch or VBS method for this question anywhere else. If I shouldn't add it to this thread (jscript), please add a comment on how/where it should go, and I will delete this answer and post as directed. :)

我导出了批处理和 VBS 方法(如下),因为我在其他任何地方都找不到针对此问题的等效批处理或 VBS 方法。如果我不应该将它添加到这个线程(jscript),请添加一个关于它应该如何/去哪里的评论,我将删除这个答案并按照指示发布。:)

Batch (single line - no carriage return):

批处理(单行-无回车):

for /f "tokens=2*" %%f in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory ^|find /i "Profiles"') do @set ProfDir=%%g

VBScript:

脚本:

' http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/registry/#ListRegFiles.htm

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath,_
 arrValueNames, arrValueTypes

For i=0 To UBound(arrValueNames)
'    StdOut.WriteLine "File Name: " & arrValueNames(i) & " -- "
    oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
    arrValueNames(i),strValue
'    StdOut.WriteLine "Location: " & strValue
'    StdOut.WriteBlankLines(1)
    IF arrValueNames(i) = "ProfilesDirectory" THEN ProfileRoot= strValue
Next

wscript.echo("ProfileRoot=" & ProfileRoot)