在2000下如何取到指定用户的桌面路径。如:我当前用AA用户登陆,但我想取administator的桌面路径
解决方案 »
- 用printer打印,如何实现右对齐
- 己知一控件句柄,怎么用API把控件的Dock设成Fill
- 在 MSHFlexGrid 控件中,怎么样用鼠标拖动列标题,改变列的顺序?
- 如何返回调用的存储过程中用到的参数!或者用msflexgrid控件显示?有分!
- VB如何读取和修改文件属性摘要中的信息
- 象棋和五子棋是怎样实现AI的?
- 请问高手--怎样用程序实现将进程里的应用程序去掉,如word等。
- 请教,以下代码为何会引起非法操作。(操作系统:win98se)
- VB里用代码怎么才能做到让它一打开记事本就运行自己写的 *.exe 文件?
- 有谁在VB里用过FrameRgn函数?
- 求救:我在Win98下调试程序,当运行到API函数WaitForSingleObject()时就死机!!
- 每次提问我都给100分,这次也不例外,顶者有分,关于垃圾的activeskin的问题
改成
C:\Documents and Settings\administator\桌面
就行了吧
好象桌面路径不能改的吧,所以
http://dev.csdn.net/develop/article/20/20520.shtm
剩下的就是简单的替换了
我的机用administrator登陆得到的桌面是
C:\Documents and Settings\Administrator.HXY\桌面
而用AA得到的是
C:\Documents and Settings\AA\桌面
用system取到的桌面是
C:\Documents and Settings\Default User.WINNT\桌面
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
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
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
Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) As Long
如果有 Administrator 的话打开下一级路径.里面直接就有Desktop 或者是桌面.