
VB 浏览文件夹获取文件夹路径
我编写了一个小程序,程序下有个file文件夹,里面有很多文件,程序运行时我会根据条件选择出一部分文件,但是程序只能选择出文件名,我希望能够将我选择出的文件名对应的文件拷贝...
我编写了一个小程序,程序下有个file文件夹,里面有很多文件,程序运行时我会根据条件选择出一部分文件,但是程序只能选择出文件名,我希望能够将我选择出的文件名对应的文件拷贝出来,拷贝的目标文件夹由我通过浏览文件夹指定,初步想通过Commanddialog来实现,我该怎么写代码?
能不能在浏览出来的文件夹中让用户自己新建文件夹,就像我们有时安装程序那样! 展开
能不能在浏览出来的文件夹中让用户自己新建文件夹,就像我们有时安装程序那样! 展开
3个回答
展开全部
commondialog控件只能选择文件,不能选择文件夹,用下面的代码可以选文件夹:
Option Explicit
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(Optional sTitle As String = "请选择文件夹") As String
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
.hWndOwner = 0 ' Me.hWnd
.lpszTitle = lstrcat(sTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList lpIDList, sPath
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
BrowseForFolder = sPath
End Function
Private Sub Command1_Click()
MsgBox BrowseForFolder()
End Sub
Option Explicit
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(Optional sTitle As String = "请选择文件夹") As String
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
.hWndOwner = 0 ' Me.hWnd
.lpszTitle = lstrcat(sTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList lpIDList, sPath
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
BrowseForFolder = sPath
End Function
Private Sub Command1_Click()
MsgBox BrowseForFolder()
End Sub
追问
谢谢!这个可以实现,再问下,能不能在浏览出来的文件夹中让用户自己新建文件夹,就像我们有时安装程序那样!
追答
Option Explicit
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const BIF_NEWDIALOGSTYLE = &H40
Const BIF_EDITBOX = &H10
Const BIF_USENEWUI = BIF_NEWDIALOGSTYLE Or BIF_EDITBOX
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(Optional sTitle As String = "请选择文件夹") As String
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
.hWndOwner = 0 ' Me.hWnd
.lpszTitle = lstrcat(sTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_USENEWUI
End With
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList lpIDList, sPath
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
BrowseForFolder = sPath
End Function
Private Sub Command1_Click()
MsgBox BrowseForFolder()
End Sub
展开全部
呵呵, xjnzhidao方法非常好!至于你要实现新建文件的功能,把xjnzhidao的代码作如下更改
:
With udtBI
.hWndOwner = 0 ' Me.hWnd
.lpszTitle = lstrcat(sTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With
改:
With udtBI
.hWndOwner = 0 ' Me.hWnd
.lpszTitle = lstrcat(sTitle, "")
.ulFlags =99'--------------------------这里是更改的地方
End With
:
With udtBI
.hWndOwner = 0 ' Me.hWnd
.lpszTitle = lstrcat(sTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With
改:
With udtBI
.hWndOwner = 0 ' Me.hWnd
.lpszTitle = lstrcat(sTitle, "")
.ulFlags =99'--------------------------这里是更改的地方
End With
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询