资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
如何用如何用 VBVB 获得获得 WindowsWindows 目录目录如何用 VB 获得 Windows 目录.txt52 每个人都一条抛物线,天赋决定其开口,而最高点则需后天的努力。没有秋日落叶的飘零,何来新春绿芽的饿明丽?只有懂得失去,才会重新拥有。现在有很多关于如何用 VB 获得 Windows 目录的文章,但大都只讲到如何获得Windows 目录和 System 目录,有时候我们却需要获得像“我的文档“这样的目录(“我的文档“的路径并不是固定的,可以由自己设定,也有可能因为系统的安装路径不同而不同) ,那又该如何处理呢?下面我们来具体谈谈如何用 VB 获得这种路径。先向大家介绍两个函数,这两个函数分别是SHGetSpecialFolderLocation 和 SHGetPathFromIDList,这就是我们用来获得各种路径的武器。 函数声明: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 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 各种目录路径的例子,供大家参考。如果您有什么问题或建议,欢迎给我来信(xuhaoliang21cn.com)。程序界面如下:程序代码如下: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, pidlSHGetPathFromIDList pidl, sTmptxtSendTo.Text = Left(sTmp, InStr(sTmp, Chr(0) - 1)*获得我的文档目录*SHGetSpecialFolderLocation 0, MYDOCUMENTS, pidlSHGetPathFromIDList pidl, sTmptxtDocument.Text = Left(sTmp, InStr(sTmp, Chr(0) - 1)*获得程序集目录*SHGetSpecialFolderLocation 0, PROGRAMS, pidlSHGetPathFromIDList pidl, sTmptxtProgram.Text = Left(sTmp, InStr(sTmp, Chr(0) - 1)*获得启动目录*SHGetSpecialFolderLocation 0, STARTUP, pidlSHGetPathFromIDList pidl, sTmptxtStart.Text = Left(sTmp, InStr(sTmp, Chr(0) - 1)*获得开始菜单目录*SHGetSpecialFolderLocation 0, STARTMENU, pidlSHGetPathFromIDList pidl, sTmptxtStartMenu.Text = Left(sTmp, InStr(sTmp, Chr(0) - 1)*获得收藏夹目录*SHGetSpecialFolderLocation 0, MYFAVORITES, pidlSHGetPathFromIDList pidl, sTmptxtFavorites.Text = Left(sTmp, InStr(sTmp, Chr(0) - 1)*获得最后打开的文件目录*SHGetSpecialFolderLocation 0, RECENT, pidlSHGetPathFromIDList pidl, sTmptxtRecent.Text = Left(sTmp, InStr(sTmp, Chr(0) - 1)*获得网上邻居目录*SHGetSpecialFolderLocation 0, NETHOOD, pidlSHGetPathFromIDList pidl, sTmptxtNetHood.Text = Left(sTmp, InStr(sTmp, Chr(0) - 1)*获得字体目录*SHGetSpecialFolderLoc
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号