VB选择文件路径代码

VB选择文件路径代码,第1张

点击“打开”按钮后,调用本函数,在出现的选择文件对话框中选择好文件,点击确定后会返回文件路径

01Function FPath$(nhwnd&, Title$)

02Dim bi As BROWSEINFO

03Dim idl As ITEMIDLIST

04Dim rtn&, pidl&, path$, pos%

05bi.hOwner = nhwnd&

06bi.pidlRoot = idl.mkid.cb

07bi.lpszTitle = Title$

08bi.ulFlags = &H1

09pidl&= SHBrowseForFolder(bi)

10path$ = Space$(512)

11rtn&= SHGetPathFromIDList(ByVal pidl&, ByVal path$)

12pos% = InStr(path$, Chr$(0))

13FPath$ = Left(path$, pos - 1)

14End Function

VB2008测试通过 Imports System Imports System.IO Imports System.Threading Imports System.Windows.Forms\x0d\x0a Public Class FrmMain\x0d\x0a\x0d\x0aDim newSearcher As getFileByName\x0d\x0a\x0d\x0a Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\x0d\x0a\x0d\x0arball.Checked = True\x0d\x0a\x0d\x0atbpath.Text = ""\x0d\x0a\x0d\x0atbpath.Enabled = False\x0d\x0a\x0d\x0abtndlg.Enabled = False\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click\x0d\x0a\x0d\x0atv.Nodes.Clear()\x0d\x0a\x0d\x0abtnOK.Enabled = False\x0d\x0a\x0d\x0abtnExit.Enabled = False\x0d\x0a\x0d\x0aIf rball.Checked Then\x0d\x0a\x0d\x0arun()\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0aIf tbpath.Text = "" Then\x0d\x0a\x0d\x0aMsgBox("请输入或者选择一个路径")\x0d\x0a\x0d\x0aExit Sub\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0arun(tbpath.Text)\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub btndlg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndlg.Click\x0d\x0a\x0d\x0afbdlg.RootFolder = Environment.SpecialFolder.MyComputer\x0d\x0a\x0d\x0afbdlg.ShowDialog()\x0d\x0a\x0d\x0atbpath.Text = fbdlg.SelectedPath\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub rbchs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbchs.CheckedChanged\x0d\x0a\x0d\x0aIf rbchs.Checked = True Then\x0d\x0a\x0d\x0atbpath.Enabled = True\x0d\x0a\x0d\x0abtndlg.Enabled = True\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0atbpath.Enabled = False\x0d\x0a\x0d\x0abtndlg.Enabled = False\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub run()\x0d\x0a\x0d\x0aIf tbfileName.Text = "" Then\x0d\x0a\x0d\x0aMsgBox("请输入文件名称")\x0d\x0a\x0d\x0abtnOK.Enabled = True\x0d\x0a\x0d\x0abtnExit.Enabled = True\x0d\x0a\x0d\x0aExit Sub\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0anewSearcher = New getFileByName(tbfileName.Text)\x0d\x0a\x0d\x0anewSearcher.GetDrivers()\x0d\x0a\x0d\x0atmr.Enabled = True\x0d\x0a\x0d\x0atmr.Interval = 10\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub run(ByVal path As String)\x0d\x0a\x0d\x0aIf tbfileName.Text = "" Then\x0d\x0a\x0d\x0aMsgBox("请输入文件名称")\x0d\x0a\x0d\x0abtnOK.Enabled = True\x0d\x0a\x0d\x0abtnExit.Enabled = True\x0d\x0a\x0d\x0aExit Sub\x0d\x0a\x0d\x0aElse\x0d\x0a\x0d\x0anewSearcher = New getFileByName(tbfileName.Text)\x0d\x0a\x0d\x0anewSearcher.SearchFolder(path)\x0d\x0a\x0d\x0atmr.Enabled = True\x0d\x0a\x0d\x0atmr.Interval = 50\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick\x0d\x0a\x0d\x0aLblshow.Text = newSearcher.currentPath\x0d\x0a\x0d\x0aIf newSearcher.cntThread = 0 And newSearcher.isDone Then\x0d\x0a\x0d\x0a'MsgBox(newSearcher.getFile.ToString)\x0d\x0a\x0d\x0a'MsgBox("全部线程结束")\x0d\x0a\x0d\x0aFor Each Str As String In newSearcher.getFile\x0d\x0a\x0d\x0atv.Nodes.Add(Str)\x0d\x0a\x0d\x0aNext\x0d\x0a\x0d\x0a'For i As Integer = 0 To newSearcher.getFile.Length - 1\x0d\x0a\x0d\x0a'tv.Nodes.Add(newSearcher.getFile(i))\x0d\x0a\x0d\x0a'Next\x0d\x0a\x0d\x0atmr.Enabled = False\x0d\x0a\x0d\x0abtnExit.Enabled = True\x0d\x0a\x0d\x0abtnOK.Enabled = True\x0d\x0a\x0d\x0aEnd If\x0d\x0a\x0d\x0aMy.Application.DoEvents()\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click\x0d\x0a\x0d\x0aMe.Close()\x0d\x0a\x0d\x0aEnd Sub End Class

添加一个文件打开对话框,在工程--部件中选择

CommonDialog1.DefaultExt       扩展名

CommonDialog1.InitDir        路径

CommonDialog1.Filter        文件过滤,按扩展名

CommonDialog1.ShowOpen     文件打开


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11599055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存