VB如何获得安装目录

来源:百度知道 编辑:UC知道 时间:2024/06/20 06:35:11
有的是C盘,有的是D盘
我想写一个按钮,一点击就可以把
*:\Windows\system32\*.jpg文件的路径直接显示到text1.text去.
最好能写出来,给我复制
因为默认路径是变动的不一定是C盘,所以要要直接获得路径

Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Sub Command1_Click()
Dim lpBuffer As String, i As Integer
lpBuffer = String(255, " ")
i = GetWindowsDirectory(lpBuffer, 255)
Print Left(lpBuffer, i) '可以设置一个变量来存储这个系统目录
End Sub

1. app.path语句可以获得本程序所在目录
2. 获取系统目录, 使用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,它决定要查找的目录是哪一个目