专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »VB教程 » vb获得系统时间:如何用VB获得Windows各类系统目录 »正文

vb获得系统时间:如何用VB获得Windows各类系统目录

来源: 发布时间:星期五, 2008年12月26日 浏览:221次 评论:0
现在有很多有关如何用VB获得Windows目录文章,但大都只讲到如何获得Windows目录和目录有时候我们却需要获得像"我文档"这样目录("我文档"路径并不是固定可以由自己设定也有可能系统安装路径区别而区别)那又该如何处理呢?下面我们来具体谈谈如何用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:获得某个特殊目录在特殊目录列表中位置;它有 3个参数个参数是用来指定所有者窗口在应用中般我们写上"0"就可以了;第 2个参数是个整数id它决定要查找目录是哪个目录取值可能如下:
&H0& '桌面
&H2& '
&H5& '我文档
&H6& '收藏夹
&H7& '启动
&H8& '最近打开文件
&H9& '发送
&HB& '开始菜单
&H13& '网上邻居
&H14& '字体
&H15& 'ShellNew
&H1A& 'Application Data
&H1B& 'PrHood
&H20& '网页临时文件
&H21& 'Cookies目录
&H22& '历史
第 3个参数是获得特殊目录在特殊目录列表中地址

SHGetPathFromIDList:根据某特殊目录在特殊目录列表中地址获取该目录准确路径它有两个参数个参数是特殊目录在特殊目录列表中地址也即上所获得地址;第 2个参数是串型数据用来保存返回特殊目录准确路径
比如:为了获得DeskTop路径首先需SHGetSpecialFolderLocation获得DeskTop在特殊目录列表中位置Pid然后SHGetPathFromIDList获得Pid指向列表内容即DeskTop准确路径

下面是我编写个用来获取Windows各种目录路径例子供大家参考如果您有什么问题或建议欢迎给我来信([email protected])

代码如下:
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 GetDirectory Lib "kernel32" Alias "GetDirectoryA" (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& 'PrHood
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)
'*************************获得目录***********************************
Length = GetDirectory(sTmp, MAX_LEN)
txt.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)
'*************************获得PrHood目录*********************************
SHGetSpecialFolderLocation 0, PRINTHOOD, pidl
SHGetPathFromIDList pidl, sTmp
txtPrHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
End Sub
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: