使用VB获得系统目录路径

使用VB获得系统目录路径,第1张

概述函数声明:  Private  Declare  Function  SHGetSpecialFolderLocation Lib  " Shell32 "  (ByVal hwndOwner  As   Long , ByVal nFolder  As   Integer , ppidl  As   Long )  As   Long   Private  Declare  Function  

函数声明:

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 LongPrivate Declare Function SHGetPathFromIDList lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pIDl As Long,ByVal szPath As String) As LongPrivate Declare Function GetwindowsDirectory lib "kernel32" Alias "GetwindowsDirectoryA" (ByVal lpBuffer As String,ByVal nSize As Long) As LongPrivate Declare Function GetSystemDirectory lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String,ByVal nSize As Long) As LongPrivate Declare Function GetTempPath lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long,ByVal lpBuffer As String) As LongConst 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& 'ShellNewConst APPDATA = &H1A& 'Application DataConst PRINTHOOD = &H1B& 'PrintHoodConst PAGETMP = &H20& '网页临时文件Const cookieS = &H21& 'cookies目录Const HISTORY = &H22& '历史Private Sub Command2_Click()EndEnd SubPrivate 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,pIDlSHGetPathFromIDList pIDl,sTmptxtDesktop.Text = left(sTmp,InStr(sTmp,Chr(0)) - 1)'*************************获得发送到目录**********************************SHGetSpecialFolderLocation 0,SENDTO,sTmptxtSendTo.Text = left(sTmp,Chr(0)) - 1)'*************************获得我的文档目录*********************************SHGetSpecialFolderLocation 0,MYdocumentS,sTmptxtdocument.Text = left(sTmp,Chr(0)) - 1)'*************************获得程序集目录***********************************SHGetSpecialFolderLocation 0,PROGRAMS,sTmptxtProgram.Text = left(sTmp,Chr(0)) - 1)'*************************获得启动目录*************************************SHGetSpecialFolderLocation 0,STARTUP,sTmptxtStart.Text = left(sTmp,Chr(0)) - 1)'*************************获得开始菜单目录*********************************SHGetSpecialFolderLocation 0,startmenu,sTmptxtstartmenu.Text = left(sTmp,Chr(0)) - 1)'*************************获得收藏夹目录***********************************SHGetSpecialFolderLocation 0,MYFAVORITES,sTmptxtFavorites.Text = left(sTmp,Chr(0)) - 1)'**********************获得最后打开的文件目录*******************************SHGetSpecialFolderLocation 0,RECENT,sTmptxtRecent.Text = left(sTmp,Chr(0)) - 1)'*************************获得网上邻居目录*********************************SHGetSpecialFolderLocation 0,NETHOOD,sTmptxtNetHood.Text = left(sTmp,Chr(0)) - 1)'*************************获得字体目录**********************************SHGetSpecialFolderLocation 0,FontS,sTmptxtFonts.Text = left(sTmp,Chr(0)) - 1)'*************************获得cookies目录**********************************SHGetSpecialFolderLocation 0,cookieS,sTmptxtcookies.Text = left(sTmp,Chr(0)) - 1)'*************************获得历史目录**********************************SHGetSpecialFolderLocation 0,HISTORY,sTmptxtHistory.Text = left(sTmp,Chr(0)) - 1)'***********************获得网页临时文件目录*******************************SHGetSpecialFolderLocation 0,PAGETMP,sTmptxtPageTmp.Text = left(sTmp,Chr(0)) - 1)'*************************获得ShellNew目录*********************************SHGetSpecialFolderLocation 0,SHELLNEW,sTmptxtShellNew.Text = left(sTmp,Chr(0)) - 1)'***********************获得Application Data目录*****************************SHGetSpecialFolderLocation 0,APPDATA,sTmptxtAppData.Text = left(sTmp,Chr(0)) - 1)'*************************获得PrintHood目录*********************************SHGetSpecialFolderLocation 0,PRINTHOOD,sTmptxtPrintHood.Text = left(sTmp,Chr(0)) - 1)End Sub
总结

以上是内存溢出为你收集整理的使用VB获得系统目录路径全部内容,希望文章能够帮你解决使用VB获得系统目录路径所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1294115.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存