How to get or retrieve different users´ desktop folder than current user´s desktop folder with VBScript?

旧巷老猫 提交于 2021-02-07 19:50:11

问题


On a Windows computer there are three users: Administrator, UserA and UserB

How to retrieve the path to UserA´s desktop folder and UserB respectively when I am logged in as Administrator?

I am looking for VBScript that will run on Windows 7 and Windows Server 2008.

I tried SpecialFolders("Desktop"), but this returns the Administrator´s desktop folder path, not the desktop folder path of UserA.

As a result I want to get the paths of C:\Users\UserA\Desktop and C:\Users\UserB\Desktop.

Also looking a way to retrieve folder Start Menu\Programs of UserA and UserB.


回答1:


(Adapted from my answer on a similar question Getting special Folder path for a given user in Jscript.)

This can't be done in pure VBcript and Windows Script Host, not without external utilities.

On the other hand, if you were to use a language supporting Windows API calls (such as C# or C++), you would be able to perform your task either a) using the SHGetKnownFolderPath function (or SHGetFolderPath on Windows versions prior to Vista), or b) by reading the path from that user's registry hive. See these questions for details:

  • Get CSIDL_LOCAL_APPDATA path for any user on Windows
  • How can I get the path of a Windows “special folder” for a specific user?
  • SHGetFolderPath() for a specific user



回答2:


Why is everyone so quick to say this cannot be done? This is very easy to do with WMI.

I've broken the script into pieces so you can see how I'm performing each step.

arrAccounts = Array("UserA", "UserB")

For Each strUser in arrAccounts
    WScript.Echo GetUserDesktop(GetSID(strUser))
Next

Function GetUserDesktop(strSID)
    Const HKEY_USERS = &H80000003

    strComputer = "."
    Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\default:StdRegProv")
    strKeyPath = strSID & "\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
    strValueName = "Desktop"
    objRegistry.GetStringValue HKEY_USERS, strKeyPath, strValueName, strValue
    GetUserDesktop = strValue
End Function

Function GetSID(strUser)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    ' Get the computer name (using WMI)
    For Each objComputer In objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
        strComputerName = objComputer.Name
        Exit For
    Next

    ' You could just as easily do this...
'   strComputerName = CreateObject("WScript.Network").ComputerName

    Set objAccount = objWMIService.Get _
        ("Win32_UserAccount.Name='" & strUser & "',Domain='" & strComputerName & "'")
    GetSID = objAccount.SID
End Function

Just be aware that Microsoft advises against using the registry to determine the location of user shell folders. There is no guarantee that this method will continue to work in future versions of Windows, but it does work in the ones you intend to target so why not use it?



来源:https://stackoverflow.com/questions/5874919/how-to-get-or-retrieve-different-users%c2%b4-desktop-folder-than-current-user%c2%b4s-deskt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!