使用VB获得系统目录路径

函数声明:

Private Declare Function SHGetSpecialFolderLocationLib " Shell32 " (ByValhwndOwner As Long ,ByValnFolder As Integer ,ppidl As Long ) As Long
Private Declare Function SHGetPathFromIDListLib " Shell32 " Alias " SHGetPathFromIDListA " (ByValpidl As Long ,ByValszPath As String ) As Long

函数功能及参数说明:
SHGetSpecialFolderLocation:获得某个特殊目录在特殊目录列表中的位置;它有三个参数,第一个参数是用来指定所有者窗口的,在应用中一般我们写上 " 0 " 就可以了;第二个参数是一个整数id,它决定要查找的目录是哪一个目录,它的取值可能如下:
& H0 & ' 桌面
& H2 & ' 程序集
& H5 & ' 我的文档
& H6 & ' 收藏夹
& H7 & ' 启动
& H8 & ' 最近打开的文件
& H9 & ' 发送
& HB & ' 开始菜单
& H13 & ' 网上邻居
& H14 & ' 字体
& H15 & ' ShellNew
& H1A & ' ApplicationData
& H1B & ' PrintHood
& H20 & ' 网页临时文件
& H21 & ' Cookies目录
& H22 & ' 历史
第三个参数是获得的特殊目录在特殊目录列表中的地址。
SHGetPathFromIDList:根据某特殊目录在特殊目录列表中的地址获取该目录的准确路径。它有两个参数,第一个参数是特殊目录在特殊目录列表中的地址,也即上一个函数所获得的地址;第二个参数是一个字符串型数据,用来保存返回的特殊目录的准确路径。
比如:为了获得DeskTop的路径,首先需调用SHGetSpecialFolderLocation获得DeskTop在特殊目录列表中的位置Pid,然后调用SHGetPathFromIDList函数获得Pid指向的列表内容,即DeskTop的准确路径。

参考代码
Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long,ByVal nFolder As Integer,ppidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long,ByVal szPath As String) As Long
Private Declare Function GetwindowsDirectory Lib "kernel32" Alias "GetwindowsDirectoryA" (ByVal lpBuffer As String,ByVal nSize As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String,ByVal nSize As Long) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long,ByVal lpBuffer As String) As Long
Const MAX_LEN = 200 '字符串最大长度
Const DESKTOP = &H0& '桌面
Const PROGRAMS = &H2& '程序集
Const MYDOCUMENTS = &H5& '我的文档
Const MYFAVORITES = &H6& '收藏夹
Const STARTUP = &H7& '启动
Const RECENT = &H8& '最近打开的文件
Const SENDTO = &H9& '发送
Const startmenu = &HB& '开始菜单
Const NETHOOD = &H13& '网上邻居
Const FONTS = &H14& '字体
Const SHELLNEW = &H15& 'ShellNew
Const APPDATA = &H1A& 'Application Data
Const PRINTHOOD = &H1B& 'PrintHood
Const PAGETMP = &H20& '网页临时文件
Const COOKIES = &H21& 'Cookies目录
Const HISTORY = &H22& '历史
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Dim sTmp As String * MAX_LEN   '存放结果的固定长度的字符串
Dim nLength As Long   '字符串的实际长度
Dim pidl As Long   '某特殊目录在特殊目录列表中的位置
'*************************获得Windows目录**********************************
Length = GetwindowsDirectory(sTmp,MAX_LEN)
txtWin.Text = Left(sTmp,Length)
'*************************获得System目录***********************************
Length = GetSystemDirectory(sTmp,MAX_LEN)
txtSystem.Text = Left(sTmp,Length)
'*************************获得Temp目录***********************************
Length = GetTempPath(MAX_LEN,sTmp)
txtTemp.Text = Left(sTmp,Length)
'*************************获得DeskTop目录**********************************
SHGetSpecialFolderLocation 0,DESKTOP,pidl
SHGetPathFromIDList pidl,sTmp
txtDesktop.Text = Left(sTmp,InStr(sTmp,Chr(0)) - 1)
'*************************获得发送到目录**********************************
SHGetSpecialFolderLocation 0,SENDTO,sTmp
txtSendTo.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得我的文档目录*********************************
SHGetSpecialFolderLocation 0,MYDOCUMENTS,sTmp
txtDocument.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得程序集目录***********************************
SHGetSpecialFolderLocation 0,PROGRAMS,sTmp
txtProgram.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得启动目录*************************************
SHGetSpecialFolderLocation 0,STARTUP,sTmp
txtStart.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得开始菜单目录*********************************
SHGetSpecialFolderLocation 0,startmenu,sTmp
txtstartmenu.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得收藏夹目录***********************************
SHGetSpecialFolderLocation 0,MYFAVORITES,sTmp
txtFavorites.Text = Left(sTmp,Chr(0)) - 1)
'**********************获得最后打开的文件目录*******************************
SHGetSpecialFolderLocation 0,RECENT,sTmp
txtRecent.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得网上邻居目录*********************************
SHGetSpecialFolderLocation 0,NETHOOD,sTmp
txtNetHood.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得字体目录**********************************
SHGetSpecialFolderLocation 0,FONTS,sTmp
txtFonts.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得Cookies目录**********************************
SHGetSpecialFolderLocation 0,COOKIES,sTmp
txtCookies.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得历史目录**********************************
SHGetSpecialFolderLocation 0,HISTORY,sTmp
txtHistory.Text = Left(sTmp,Chr(0)) - 1)
'***********************获得网页临时文件目录*******************************
SHGetSpecialFolderLocation 0,PAGETMP,sTmp
txtPageTmp.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得ShellNew目录*********************************
SHGetSpecialFolderLocation 0,SHELLNEW,sTmp
txtShellNew.Text = Left(sTmp,Chr(0)) - 1)
'***********************获得Application Data目录*****************************
SHGetSpecialFolderLocation 0,APPDATA,sTmp
txtAppData.Text = Left(sTmp,Chr(0)) - 1)
'*************************获得PrintHood目录*********************************
SHGetSpecialFolderLocation 0,PRINTHOOD,sTmp
txtPrintHood.Text = Left(sTmp,Chr(0)) - 1)
End Sub

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...