如何用VB獲得Windows各類系統目錄
發表于:2007-07-14來源:作者:點擊數:
標簽:
現在有很多關于如何用 VB 獲得 Windows 目錄的文章,但大都只講到如何獲得Windows目錄和System目錄,有時候我們卻需要獲得像我的文檔這樣的目錄(我的文檔的路徑并不是固定的,可以由自己設定,也有可能因為系統的安裝路徑不同而不同),那又該如何處理呢?下
現在有很多關于如何用
VB獲得
Windows目錄的文章,但大都只講到如何獲得Windows目錄和System目錄,有時候我們卻需要獲得像"我的文檔"這樣的目錄("我的文檔"的路徑并不是固定的,可以由自己設定,也有可能因為系統的安裝路徑不同而不同),那又該如何處理呢?下面我們來具體談談如何用VB獲得這種路徑。
先向大家介紹兩個API函數,這兩個函數分別是SHGetSpecialFolderLocation和SHGetPathFromIDList,這就是我們用來獲得各種路徑的武器。
函數聲明:
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
函數功能及參數說明:
SHGetSpecialFolderLocation:獲得某個特殊目錄在特殊目錄列表中的位置;它有三個參數,第一個參數是用來指定所有者窗口的,在應用中一般我們寫上"0"就可以了;第二個參數是一個整數id,它決定要查找的目錄是哪一個目錄,它的取值可能如下:
&H0& '桌面
&H2& '程序集
&H5& '我的文檔
&H6& '收藏夾
&H7& '啟動
&H8& '最近打開的文件
&H9& '發送
&HB& '開始菜單
&H13& '網上鄰居
&H14& '字體
&H15& 'ShellNew
&H1A& 'Application Data
&H1B& 'PrintHood
&H20& '網頁臨時文件
&H21& 'Cookies目錄
&H22& '歷史
第三個參數是獲得的特殊目錄在特殊目錄列表中的地址。
SHGetPathFromIDList:根據某特殊目錄在特殊目錄列表中的地址獲取該目錄的準確路徑。它有兩個參數,第一個參數是特殊目錄在特殊目錄列表中的地址,也即上一個函數所獲得的地址;第二個參數是一個字符串型數據,用來保存返回的特殊目錄的準確路徑。
比如:為了獲得DeskTop的路徑,首先需調用SHGetSpecialFolderLocation獲得DeskTop在特殊目錄列表中的位置Pid,然后調用SHGetPathFromIDList函數獲得Pid指向的列表內容,即DeskTop的準確路徑。
下面是我編寫的一個用來獲取Windows各種目錄路徑的例子,供大家參考。如果您有什么問題或建議,歡迎給我來信(xuhaoliang@21cn.com)。
程序代碼如下:
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, pidl
SHGetPathFromIDList pidl, sTmp
txtSendTo.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得我的文檔目錄*********************************
SHGetSpecialFolderLocation 0, MYDOCUMENTS, pidl
SHGetPathFromIDList pidl, sTmp
txtDocument.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得程序集目錄***********************************
SHGetSpecialFolderLocation 0, PROGRAMS, pidl
SHGetPathFromIDList pidl, sTmp
txtProgram.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得啟動目錄*************************************
SHGetSpecialFolderLocation 0, STARTUP, pidl
SHGetPathFromIDList pidl, sTmp
txtStart.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得開始菜單目錄*********************************
SHGetSpecialFolderLocation 0, STARTMENU, pidl
SHGetPathFromIDList pidl, sTmp
txtStartMenu.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得收藏夾目錄***********************************
SHGetSpecialFolderLocation 0, MYFAVORITES, pidl
SHGetPathFromIDList pidl, sTmp
txtFavorites.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'**********************獲得最后打開的文件目錄*******************************
SHGetSpecialFolderLocation 0, RECENT, pidl
SHGetPathFromIDList pidl, sTmp
txtRecent.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得網上鄰居目錄*********************************
SHGetSpecialFolderLocation 0, NETHOOD, pidl
SHGetPathFromIDList pidl, sTmp
txtNetHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得字體目錄**********************************
SHGetSpecialFolderLocation 0, FONTS, pidl
SHGetPathFromIDList pidl, sTmp
txtFonts.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得Cookies目錄**********************************
SHGetSpecialFolderLocation 0, COOKIES, pidl
SHGetPathFromIDList pidl, sTmp
txtCookies.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得歷史目錄**********************************
SHGetSpecialFolderLocation 0, HISTORY, pidl
SHGetPathFromIDList pidl, sTmp
txtHistory.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'***********************獲得網頁臨時文件目錄*******************************
SHGetSpecialFolderLocation 0, PAGETMP, pidl
SHGetPathFromIDList pidl, sTmp
txtPageTmp.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得ShellNew目錄*********************************
SHGetSpecialFolderLocation 0, SHELLNEW, pidl
SHGetPathFromIDList pidl, sTmp
txtShellNew.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'***********************獲得Application Data目錄*****************************
SHGetSpecialFolderLocation 0, APPDATA, pidl
SHGetPathFromIDList pidl, sTmp
txtAppData.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************獲得PrintHood目錄*********************************
SHGetSpecialFolderLocation 0, PRINTHOOD, pidl
SHGetPathFromIDList pidl, sTmp
txtPrintHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
End Sub
原文轉自:http://www.anti-gravitydesign.com