×

vb浏览选择文件

vb浏览选择文件(vb选择文件路径怎么写)

admin admin 发表于2023-03-29 22:32:07 浏览40 评论0

抢沙发发表评论

本文目录一览:

VB6.0浏览文件夹对话框,怎么做?

浏览文件夹窗口需要使用WINDOWS

API函数:这个是我自己的程序当中的一段代码:{

BROWSEINFO

bi;

ZeroMemory(bi,

sizeof(BROWSEINFO));

bi.hwndOwner

=

m_hWnd;

bi.ulFlags

=

BIF_RETURNONLYFSDIRS;

LPITEMIDLIST

pidl

=

SHBrowseForFolder(bi);

BOOL

bRet

=

FALSE;

TCHAR

szFolder[MAX_PATH*2];

szFolder[0]

=

_T('\0');

if

(pidl)

{

if

(SHGetPathFromIDList(pidl,

szFolder))

bRet

=

TRUE;

IMalloc

*pMalloc

=

NULL;

if

(SUCCEEDED(SHGetMalloc(pMalloc))

pMalloc)

{

pMalloc-Free(pidl);

pMalloc-Release();

}

}

UpdateData(1);

m_Path=szFolder;

UpdateData(0);

}这个代码添加在BUTTON的ON事件函数中,目的是将文件夹路径输出到m_Path这个TEXTBOX控件中。你略微改改名即可使用。

VB 怎么制作“浏览”按扭,可以浏览电脑上的文件和文件夹,将浏览选择好后的地址显示在text1控制中。

给你提供几种方法,第一个如一楼那样,使用公用对话框,选择文件。第二你可以使用VB提供的驱动器列表,文件夹列表,文件列表来完成此操作,非常简单,如需代码可以联系我。第三可以通过API调用Windows的浏览文件夹对话框,给你如下代码,添加到标准模块里,需要时只要调用就行。-vb浏览选择文件

'这个模块利用Shell API函数弹出文件夹浏览窗口

Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _

"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Private Declare Function SHGetSpecialFolderLocation Lib _

"shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder _

As Long, pIdl As ITEMIDLIST) As Long

Private Declare Function SHGetFileInfo Lib "Shell32" Alias _

"SHGetFileInfoA" (ByVal pszPath As Any, ByVal _

dwFileAttributes As Long, psfi As SHFILEINFO, ByVal _

cbFileInfo As Long, ByVal uFlags As Long) As Long

Private Declare Function ShellAbout Lib "shell32.dll" Alias _

"ShellAboutA" (ByVal hwnd As Long, ByVal szApp As _

String, ByVal szOtherStuff As String, ByVal hIcon As Long) _

As Long

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _

Alias "SHGetPathFromIDListA" (ByVal pIdl As Long, ByVal _

pszPath As String) As Long

Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)

Const MAX_PATH = 260

Private Type SHITEMID

cb As Long

abID() As Byte

End Type

Private Type ITEMIDLIST

mkid As SHITEMID

End Type

Private Type BROWSEINFO

hOwner As Long

pidlRoot As Long

pszDisplayName As String

lpszTitle As String

ulFlags As Long

lpfn As Long

lParam As Long

iImage As Long

End Type

Private Type SHFILEINFO

hIcon As Long

iIcon As Long

dwAttributes As Long

szDisplayName As String * MAX_PATH

szTypeName As String * 80

End Type

Private Function GetFolderValue(wIdx As Integer) As Long

If wIdx 2 Then

GetFolderValue = 0

ElseIf wIdx 12 Then

GetFolderValue = wIdx

Else

GetFolderValue = wIdx + 4

End If

End Function

Public Function SeleteFolder(ByVal frmX As Form)

'参数frmX为调用文件夹浏览窗口的窗体

Dim BI As BROWSEINFO

Dim nFolder As Long

Dim IDL As ITEMIDLIST

Dim pIdl As Long

Dim sPath As String

Dim SHFI As SHFILEINFO

Dim m_wCurOptIdx As Integer

Dim txtPath As String

Dim txtDisplayNafrmx As String

With BI

.hOwner = frmX.hwnd

nFolder = GetFolderValue(m_wCurOptIdx)

If SHGetSpecialFolderLocation(ByVal frmX.hwnd, ByVal nFolder, IDL) = NOERROR Then

.pidlRoot = IDL.mkid.cb

End If

.pszDisplayName = String$(MAX_PATH, 0)

.lpszTitle = "请选择文件夹: "

.ulFlags = 0

End With

txtPath = ""

txtDisplayName = ""

pIdl = SHBrowseForFolder(BI)

If pIdl = 0 Then Exit Function

sPath = String$(MAX_PATH, 0)

SHGetPathFromIDList ByVal pIdl, ByVal sPath

txtPath = Left(sPath, InStr(sPath, vbNullChar) - 1)

'Debug.Print txtPath

'txtDisplayName = Left$(BI.pszDisplayName, _

InStr(BI.pszDisplayName, vbNullChar) - 1)

'SHGetFileInfo ByVal pIdl, 0, SHFI, Len(SHFI), _

SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SMALLICON

'SHGetFileInfo ByVal pIdl, 0, SHFI, Len(SHFI), _

SHGFI_PIDL Or SHGFI_ICON

'CoTaskMemFree pIdl

SeleteFolder = txtPath

'MsgBox "你选择的文件夹是" + Chr(13) + Chr(10) + txtPath

End Function

Vb中实现浏览文件夹在打开想要的文件

VB 打开浏览文件夹

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

Private Const MAX_PATH = 260

Private Const BIF_RETURNONLYFSDIRS = 1

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-vb浏览选择文件

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-vb浏览选择文件

Private Sub Command1_Click()

Dim iNull As Integer, lpIDList As Long

Dim sPath As String, udtBI As BrowseInfo

With udtBI

'设置浏览窗口

.hWndOwner = Me.hWnd

'返回选中的目录

.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

Text1.Text = sPath

End Sub

sPath 是返回的路径