在2000下如何取到指定用户的桌面路径。如:我当前用AA用户登陆,但我想取administator的桌面路径

解决方案 »

  1.   

    C:\Documents and Settings\AA\桌面
    改成
    C:\Documents and Settings\administator\桌面
    就行了吧
    好象桌面路径不能改的吧,所以
      

  2.   

    关于当前用户的桌面路径获取,参考:
    http://dev.csdn.net/develop/article/20/20520.shtm
    剩下的就是简单的替换了
      

  3.   

    不行啊.
    我的机用administrator登陆得到的桌面是
    C:\Documents and Settings\Administrator.HXY\桌面
    而用AA得到的是
    C:\Documents and Settings\AA\桌面
    用system取到的桌面是
    C:\Documents and Settings\Default User.WINNT\桌面
      

  4.   

    用以下几个API
    Private Const TOKEN_QUERY = (&H8)
    Private Declare Function GetAllUsersProfileDirectory Lib "userenv.dll" Alias "GetAllUsersProfileDirectoryA" (ByVal lpProfileDir As String, lpcchSize As Long) As Boolean
    Private Declare Function GetDefaultUserProfileDirectory Lib "userenv.dll" Alias "GetDefaultUserProfileDirectoryA" (ByVal lpProfileDir As String, lpcchSize As Long) As Boolean
    Private Declare Function GetProfilesDirectory Lib "userenv.dll" Alias "GetProfilesDirectoryA" (ByVal lpProfileDir As String, lpcchSize As Long) As Boolean
    Private Declare Function GetUserProfileDirectory Lib "userenv.dll" Alias "GetUserProfileDirectoryA" (ByVal hToken As Long, ByVal lpProfileDir As String, lpcchSize As Long) As Boolean
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    Private Sub Form_Load()    Dim sBuffer As String, Ret As Long, hToken As Long
        'set the graphics mode of this form to 'persistent'
        Me.AutoRedraw = True
        'create a string buffer
        sBuffer = String(255, 0)
        'open the token of the current process
        OpenProcessToken GetCurrentProcess, TOKEN_QUERY, hToken
        'retrieve this users profile directory
        GetUserProfileDirectory hToken, sBuffer, 255
        'show the result
        Me.Print StripTerminator(sBuffer)
    End Sub
    'strips off the trailing Chr$(0)'s
    Function StripTerminator(sInput As String) As String
        Dim ZeroPos As Long
        ZeroPos = InStr(1, sInput, Chr$(0))
        If ZeroPos > 0 Then
            StripTerminator = Left$(sInput, ZeroPos - 1)
        Else
            StripTerminator = sInput
        End If
    End Function
      

  5.   

    Const CSIDL_DESKTOP = &H0
    Const CSIDL_PROGRAMS = &H2
    Const CSIDL_CONTROLS = &H3
    Const CSIDL_PRINTERS = &H4
    Const CSIDL_PERSONAL = &H5
    Const CSIDL_FAVORITES = &H6
    Const CSIDL_STARTUP = &H7
    Const CSIDL_RECENT = &H8
    Const CSIDL_SENDTO = &H9
    Const CSIDL_BITBUCKET = &HA
    Const CSIDL_STARTMENU = &HB
    Const CSIDL_DESKTOPDIRECTORY = &H10
    Const CSIDL_DRIVES = &H11
    Const CSIDL_NETWORK = &H12
    Const CSIDL_NETHOOD = &H13
    Const CSIDL_FONTS = &H14
    Const CSIDL_TEMPLATES = &H15
    Const MAX_PATH = 260
    Private Type SHITEMID
        cb As Long
        abID As Byte
    End Type
    Private Type ITEMIDLIST
        mkid As SHITEMID
    End Type
    Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Sub Form_Load()
        Me.AutoRedraw = True
        'Print the folders to the form
        Me.Print "Start menu folder: " + GetSpecialfolder(CSIDL_STARTMENU)
        Me.Print "Favorites folder: " + GetSpecialfolder(CSIDL_FAVORITES)
        Me.Print "Programs folder: " + GetSpecialfolder(CSIDL_PROGRAMS)
        Me.Print "Desktop folder: " + GetSpecialfolder(CSIDL_DESKTOP)
    End Sub
    Private Function GetSpecialfolder(CSIDL As Long) As String
        Dim r As Long
        Dim IDL As ITEMIDLIST
        'Get the special folder
        r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
        If r = NOERROR Then
            'Create a buffer
            Path$ = Space$(512)
            'Get the path from the IDList
            r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$)
            'Remove the unnecessary chr$(0)'s
            GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
            Exit Function
        End If
        GetSpecialfolder = ""
    End Function
      

  6.   

    通过从PIDL中获取路径常量定义
    Public Const CSIDL_DESKTOP = &H0API定义Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
                                  (ByVal hwndOwner As Long, _
                                  ByVal nFolder As Long, _
                                  pidl As Long) As Long
    '函数定义
    Public Function GetPIDLFromSpecialFolderID(hOwner As Long, nFolder As Integer) As Long
      Dim pidl As Long
      If SHGetSpecialFolderLocation(hOwner, nFolder, pidl)>0 Then
        GetPIDLFromSpecialFolderID = pidl
      End If
    End Function'获取桌面的PIDLpidlDesktop = GetPIDLFromSpecialFolderID(0, CSIDL_DESKTOP)'其中第一个参数0,可以用载体的hwnd替换'通过PIDL获得桌面路径桌面路径=GetPathFromPIDL(pidlDesktop)Public Function GetPathFromPIDL(pidl As Long) As String
      Dim sPath As String * MAX_PATH   ' 260
      If (SHGetPathFromIDList(pidl, sPath))>0 Then
        GetPathFromPIDL = TrimExt(sPath)
      End If
    End FunctionPublic Function TrimExt(sName As String) As String
       '将定长字符串进行处理
       ' Right trim string at first null.
       Dim x As Integer
       x = InStr(sName, vbNullChar)
       If x > 0 Then TrimExt = Left$(sName, x - 1) Else TrimExt = sName
    End Function
      

  7.   

    忘了补一个API的定义了
    Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
                                  (ByVal pidl As Long, _
                                  ByVal pszPath As String) As Long
      

  8.   

    获取C:\Documents and Settings 下文件夹的名字..
    如果有 Administrator 的话打开下一级路径.里面直接就有Desktop 或者是桌面.