使用VB获得系统目录路径
1 两个函数分别是SHGetSpecialFolderLocation和SHGetPathFromIDList,这就是我们用来获得各种路径的武器。
2 函数声明:
3 Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
4 Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
5 函数功能及参数说明:
6 SHGetSpecialFolderLocation:获得某个特殊目录在特殊目录列表中的位置;它有三个参数,第一个参数是用来指定所有者窗口的,在应用中一般我们写上"0"就可以了;第二个参数是一个整数id,它决定要查找的目录是哪一个目录,它的取值可能如下:
7 &H0& '桌面
8 &H2& '程序集
9 &H5& '我的文档
10 &H6& '收藏夹
11 &H7& '启动
12 &H8& '最近打开的文件
13 &H9& '发送
14 &HB& '开始菜单
15 &H13& '网上邻居
16 &H14& '字体
17 &H15& 'ShellNew
18 &H1A& 'Application Data
19 &H1B& 'PrintHood
20 &H20& '网页临时文件
21 &H21& 'Cookies目录
22 &H22& '历史
23 第三个参数是获得的特殊目录在特殊目录列表中的地址。
24 SHGetPathFromIDList:根据某特殊目录在特殊目录列表中的地址获取该目录的准确路径。它有两个参数,第一个参数是特殊目录在特殊目录列表中的地址,也即上一个函数所获得的地址;第二个参数是一个字符串型数据,用来保存返回的特殊目录的准确路径。
25 比如:为了获得DeskTop的路径,首先需调用SHGetSpecialFolderLocation获得DeskTop在特殊目录列表中的位置Pid,然后调用SHGetPathFromIDList函数获得Pid指向的列表内容,即DeskTop的准确路径。
26 下面是我编写的一个用来获取Windows各种目录路径的例子,供大家参考。如果您有什么问题或建议,欢迎给我来信(xuhaoliang@21cn.com)。
27 程序界面如下:
28 ­
29 程序代码如下:
30 Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
31 Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
32 Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
33 Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
34 Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
35 Const MAX_LEN = 200 '字符串最大长度
36 Const DESKTOP = &H0& '桌面
37 Const PROGRAMS = &H2& '程序集
38 Const MYDOCUMENTS = &H5& '我的文档
39 Const MYFAVORITES = &H6& '收藏夹
40 Const STARTUP = &H7& '启动
41 Const RECENT = &H8& '最近打开的文件
42 Const SENDTO = &H9& '发送
43 Const STARTMENU = &HB& '开始菜单
44 Const NETHOOD = &H13& '网上邻居
45 Const FONTS = &H14& '字体
46 Const SHELLNEW = &H15& 'ShellNew
47 Const APPDATA = &H1A& 'Application Data
48 Const PRINTHOOD = &H1B& 'PrintHood
49 Const PAGETMP = &H20& '网页临时文件
50 Const COOKIES = &H21& 'Cookies目录
51 Const HISTORY = &H22& '历史
52 Private Sub Command2_Click()
53 End
54 End Sub
55 Private Sub Form_Load()
56 Dim sTmp As String * MAX_LEN '存放结果的固定长度的字符串
57 Dim nLength As Long '字符串的实际长度
58 Dim pidl As Long '某特殊目录在特殊目录列表中的位置
59 '*************************获得Windows目录**********************************
60 Length = GetWindowsDirectory(sTmp, MAX_LEN)
61 txtWin.Text = Left(sTmp, Length)
62 '*************************获得System目录***********************************
63 Length = GetSystemDirectory(sTmp, MAX_LEN)
64 txtSystem.Text = Left(sTmp, Length)
65 '*************************获得Temp目录***********************************
66 Length = GetTempPath(MAX_LEN, sTmp)
67 txtTemp.Text = Left(sTmp, Length)
68 '*************************获得DeskTop目录**********************************
69 SHGetSpecialFolderLocation 0, DESKTOP, pidl
70 SHGetPathFromIDList pidl, sTmp
71 txtDesktop.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
72 '*************************获得发送到目录**********************************
73 SHGetSpecialFolderLocation 0, SENDTO, pidl
74 SHGetPathFromIDList pidl, sTmp
75 txtSendTo.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
76 '*************************获得我的文档目录*********************************
77 SHGetSpecialFolderLocation 0, MYDOCUMENTS, pidl
78 SHGetPathFromIDList pidl, sTmp
79 txtDocument.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
80 '*************************获得程序集目录***********************************
81 SHGetSpecialFolderLocation 0, PROGRAMS, pidl
82 SHGetPathFromIDList pidl, sTmp
83 txtProgram.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
84 '*************************获得启动目录*************************************
85 SHGetSpecialFolderLocation 0, STARTUP, pidl
86 SHGetPathFromIDList pidl, sTmp
87 txtStart.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
88 '*************************获得开始菜单目录*********************************
89 SHGetSpecialFolderLocation 0, STARTMENU, pidl
90 SHGetPathFromIDList pidl, sTmp
91 txtStartMenu.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
92 '*************************获得收藏夹目录***********************************
93 SHGetSpecialFolderLocation 0, MYFAVORITES, pidl
94 SHGetPathFromIDList pidl, sTmp
95 txtFavorites.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
96 '**********************获得最后打开的文件目录*******************************
97 SHGetSpecialFolderLocation 0, RECENT, pidl
98 SHGetPathFromIDList pidl, sTmp
99 txtRecent.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
100 '*************************获得网上邻居目录*********************************
101 SHGetSpecialFolderLocation 0, NETHOOD, pidl
102 SHGetPathFromIDList pidl, sTmp
103 txtNetHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
104 '*************************获得字体目录**********************************
105 SHGetSpecialFolderLocation 0, FONTS, pidl
106 SHGetPathFromIDList pidl, sTmp
107 txtFonts.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
108 '*************************获得Cookies目录**********************************
109 SHGetSpecialFolderLocation 0, COOKIES, pidl
110 SHGetPathFromIDList pidl, sTmp
111 txtCookies.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
112 '*************************获得历史目录**********************************
113 SHGetSpecialFolderLocation 0, HISTORY, pidl
114 SHGetPathFromIDList pidl, sTmp
115 txtHistory.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
116 '***********************获得网页临时文件目录*******************************
117 SHGetSpecialFolderLocation 0, PAGETMP, pidl
118 SHGetPathFromIDList pidl, sTmp
119 txtPageTmp.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
120 '*************************获得ShellNew目录*********************************
121 SHGetSpecialFolderLocation 0, SHELLNEW, pidl
122 SHGetPathFromIDList pidl, sTmp
123 txtShellNew.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
124 '***********************获得Application Data目录*****************************
125 SHGetSpecialFolderLocation 0, APPDATA, pidl
126 SHGetPathFromIDList pidl, sTmp
127 txtAppData.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
128 '*************************获得PrintHood目录*********************************
129 SHGetSpecialFolderLocation 0, PRINTHOOD, pidl
130 SHGetPathFromIDList pidl, sTmp
131 txtPrintHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
132 End Sub
2 函数声明:
3 Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
4 Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
5 函数功能及参数说明:
6 SHGetSpecialFolderLocation:获得某个特殊目录在特殊目录列表中的位置;它有三个参数,第一个参数是用来指定所有者窗口的,在应用中一般我们写上"0"就可以了;第二个参数是一个整数id,它决定要查找的目录是哪一个目录,它的取值可能如下:
7 &H0& '桌面
8 &H2& '程序集
9 &H5& '我的文档
10 &H6& '收藏夹
11 &H7& '启动
12 &H8& '最近打开的文件
13 &H9& '发送
14 &HB& '开始菜单
15 &H13& '网上邻居
16 &H14& '字体
17 &H15& 'ShellNew
18 &H1A& 'Application Data
19 &H1B& 'PrintHood
20 &H20& '网页临时文件
21 &H21& 'Cookies目录
22 &H22& '历史
23 第三个参数是获得的特殊目录在特殊目录列表中的地址。
24 SHGetPathFromIDList:根据某特殊目录在特殊目录列表中的地址获取该目录的准确路径。它有两个参数,第一个参数是特殊目录在特殊目录列表中的地址,也即上一个函数所获得的地址;第二个参数是一个字符串型数据,用来保存返回的特殊目录的准确路径。
25 比如:为了获得DeskTop的路径,首先需调用SHGetSpecialFolderLocation获得DeskTop在特殊目录列表中的位置Pid,然后调用SHGetPathFromIDList函数获得Pid指向的列表内容,即DeskTop的准确路径。
26 下面是我编写的一个用来获取Windows各种目录路径的例子,供大家参考。如果您有什么问题或建议,欢迎给我来信(xuhaoliang@21cn.com)。
27 程序界面如下:
28 ­
29 程序代码如下:
30 Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
31 Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
32 Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
33 Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
34 Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
35 Const MAX_LEN = 200 '字符串最大长度
36 Const DESKTOP = &H0& '桌面
37 Const PROGRAMS = &H2& '程序集
38 Const MYDOCUMENTS = &H5& '我的文档
39 Const MYFAVORITES = &H6& '收藏夹
40 Const STARTUP = &H7& '启动
41 Const RECENT = &H8& '最近打开的文件
42 Const SENDTO = &H9& '发送
43 Const STARTMENU = &HB& '开始菜单
44 Const NETHOOD = &H13& '网上邻居
45 Const FONTS = &H14& '字体
46 Const SHELLNEW = &H15& 'ShellNew
47 Const APPDATA = &H1A& 'Application Data
48 Const PRINTHOOD = &H1B& 'PrintHood
49 Const PAGETMP = &H20& '网页临时文件
50 Const COOKIES = &H21& 'Cookies目录
51 Const HISTORY = &H22& '历史
52 Private Sub Command2_Click()
53 End
54 End Sub
55 Private Sub Form_Load()
56 Dim sTmp As String * MAX_LEN '存放结果的固定长度的字符串
57 Dim nLength As Long '字符串的实际长度
58 Dim pidl As Long '某特殊目录在特殊目录列表中的位置
59 '*************************获得Windows目录**********************************
60 Length = GetWindowsDirectory(sTmp, MAX_LEN)
61 txtWin.Text = Left(sTmp, Length)
62 '*************************获得System目录***********************************
63 Length = GetSystemDirectory(sTmp, MAX_LEN)
64 txtSystem.Text = Left(sTmp, Length)
65 '*************************获得Temp目录***********************************
66 Length = GetTempPath(MAX_LEN, sTmp)
67 txtTemp.Text = Left(sTmp, Length)
68 '*************************获得DeskTop目录**********************************
69 SHGetSpecialFolderLocation 0, DESKTOP, pidl
70 SHGetPathFromIDList pidl, sTmp
71 txtDesktop.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
72 '*************************获得发送到目录**********************************
73 SHGetSpecialFolderLocation 0, SENDTO, pidl
74 SHGetPathFromIDList pidl, sTmp
75 txtSendTo.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
76 '*************************获得我的文档目录*********************************
77 SHGetSpecialFolderLocation 0, MYDOCUMENTS, pidl
78 SHGetPathFromIDList pidl, sTmp
79 txtDocument.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
80 '*************************获得程序集目录***********************************
81 SHGetSpecialFolderLocation 0, PROGRAMS, pidl
82 SHGetPathFromIDList pidl, sTmp
83 txtProgram.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
84 '*************************获得启动目录*************************************
85 SHGetSpecialFolderLocation 0, STARTUP, pidl
86 SHGetPathFromIDList pidl, sTmp
87 txtStart.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
88 '*************************获得开始菜单目录*********************************
89 SHGetSpecialFolderLocation 0, STARTMENU, pidl
90 SHGetPathFromIDList pidl, sTmp
91 txtStartMenu.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
92 '*************************获得收藏夹目录***********************************
93 SHGetSpecialFolderLocation 0, MYFAVORITES, pidl
94 SHGetPathFromIDList pidl, sTmp
95 txtFavorites.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
96 '**********************获得最后打开的文件目录*******************************
97 SHGetSpecialFolderLocation 0, RECENT, pidl
98 SHGetPathFromIDList pidl, sTmp
99 txtRecent.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
100 '*************************获得网上邻居目录*********************************
101 SHGetSpecialFolderLocation 0, NETHOOD, pidl
102 SHGetPathFromIDList pidl, sTmp
103 txtNetHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
104 '*************************获得字体目录**********************************
105 SHGetSpecialFolderLocation 0, FONTS, pidl
106 SHGetPathFromIDList pidl, sTmp
107 txtFonts.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
108 '*************************获得Cookies目录**********************************
109 SHGetSpecialFolderLocation 0, COOKIES, pidl
110 SHGetPathFromIDList pidl, sTmp
111 txtCookies.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
112 '*************************获得历史目录**********************************
113 SHGetSpecialFolderLocation 0, HISTORY, pidl
114 SHGetPathFromIDList pidl, sTmp
115 txtHistory.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
116 '***********************获得网页临时文件目录*******************************
117 SHGetSpecialFolderLocation 0, PAGETMP, pidl
118 SHGetPathFromIDList pidl, sTmp
119 txtPageTmp.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
120 '*************************获得ShellNew目录*********************************
121 SHGetSpecialFolderLocation 0, SHELLNEW, pidl
122 SHGetPathFromIDList pidl, sTmp
123 txtShellNew.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
124 '***********************获得Application Data目录*****************************
125 SHGetSpecialFolderLocation 0, APPDATA, pidl
126 SHGetPathFromIDList pidl, sTmp
127 txtAppData.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
128 '*************************获得PrintHood目录*********************************
129 SHGetSpecialFolderLocation 0, PRINTHOOD, pidl
130 SHGetPathFromIDList pidl, sTmp
131 txtPrintHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
132 End Sub