当前位置:编程学习 > VB >>

VB获取文件路径

有按钮1和text1.实现:单击按钮1弹出选择文件选择框(类似记事本的打开按钮)。选择文件后在text1中显示文件的绝对路径
追问:来自手机问问action属性是什么功能?我现在在外面,麻烦解一下
答案:需要添加一个CommonDialog控件。
添加方法:按Ctrl+T,在弹出的对话框中选择Microsoft Common Dialog Control 6.0,单击确定,将该控件添加到工具箱中,然后将该控件画到窗体上。设置控件名称为cd。
编译器执行到cd.Action = 1,就会打开记事本中的“打开”对话框。待用户选择一个文件后,cd的FileName属性就会变为要打开的文件的路径名。
具体代码如下:
Private Sub Command1_Click()
    cd.Action = 1
    Text1 = cd.FileName
End Sub
望楼主采纳!!
1、新建工程,并添加TextBox,CommandButton控件。
2、按Ctrl+R,在弹出的部件窗口中勾选Microsoft Common Dialog Control 6.0,单击确定,并添加一个CommonDialog控件。
3、在CommandButton中添加以下代码:


Private Sub Command1_Click()
With CommonDialog1
.DialogTitle = "请选择文件"
.Filter = "*.*|*.*"
.CancelError = False
.ShowOpen
Text1.Text = .FileName
End With
End Sub
希望这个能帮到你。
VB2008测试通过  Imports System  Imports System.IO  Imports System.Threading  Imports System.Windows.Forms
Public Class FrmMain

Dim newSearcher As getFileByName

Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


rball.Checked = True


tbpath.Text = ""


tbpath.Enabled = False


btndlg.Enabled = False

End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click


tv.Nodes.Clear()


btnOK.Enabled = False


btnExit.Enabled = False


If rball.Checked Then



run()


Else



If tbpath.Text = "" Then





MsgBox("请输入或者选择一个路径")





Exit Sub



Else





run(tbpath.Text)



End If


End If

End Sub

Private Sub btndlg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndlg.Click


fbdlg.RootFolder = Environment.SpecialFolder.MyComputer


fbdlg.ShowDialog()


tbpath.Text = fbdlg.SelectedPath

End Sub

Private Sub rbchs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbchs.CheckedChanged


If rbchs.Checked = True Then



tbpath.Enabled = True



btndlg.Enabled = True


Else



tbpath.Enabled = False



btndlg.Enabled = False


End If

End Sub

Private Sub run()


If tbfileName.Text = "" Then



MsgBox("请输入文件名称")



btnOK.Enabled = True



btnExit.Enabled = True



Exit Sub


Else



newSearcher = New getFileByName(tbfileName.Text)



newSearcher.GetDrivers()



tmr.Enabled = True



tmr.Interval = 10


End If

End Sub

Private Sub run(ByVal path As String)


If tbfileName.Text = "" Then



MsgBox("请输入文件名称")



btnOK.Enabled = True



btnExit.Enabled = True



Exit Sub


Else



newSearcher = New getFileByName(tbfileName.Text)



newSearcher.SearchFolder(path)



tmr.Enabled = True



tmr.Interval = 50


End If

End Sub

Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick


Lblshow.Text = newSearcher.currentPath


If newSearcher.cntThread = 0 And newSearcher.isDone Then



'MsgBox(newSearcher.getFile.ToString)



'MsgBox("全部线程结束")



For Each Str As String In newSearcher.getFile





tv.Nodes.Add(Str)



Next



'For i As Integer = 0 To newSearcher.getFile.Length - 1



'tv.Nodes.Add(newSearcher.getFile(i))



'Next



tmr.Enabled = False



btnExit.Enabled = True



btnOK.Enabled = True


End If


My.Application.DoEvents()

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click


Me.Close()

End Sub End Class
Private Sub Command1_Click()
Dim oDLG
Set oDLG = CreateObject("MSComDlg.CommonDialog")
With oDLG
.DialogTitle = "打开文件"
.Filter = "所有文件|*.*"
.MaxFileSize = 255
.ShowOpen
If .FileName <> "" Then
Text1.text= .FileName
End If
End With
Set oDLG = Nothing
End Sub
添加COMOON DIALOG控件

发一个不添加对象那么麻烦的代码,用纯代码创建

另外发一个我写的函数集,不能说太全,但是很多自己看着用..

http://www.autobatch.org/bbs/viewthread.php?tid=841

 

'以下为自定义函数,到时候调用就用showopen

public function showopen(byval typeval,optional byval dirval)
cd.initialdir=dirval
cd.filter=typeval
cd.showopen
showopen=cd.filename
if showopen="" then exit function
end function

 

 

'例如

msgbox showopen("all files|*.*","c:\")

private sub command1_click()

dim filepath as string
CommonDialog1.Filter = "*.*"

CommonDialog1.ShowOpen

filepath = CommonDialog1.FileName
text1.text=filepath
end sub
'加入command1,text1,以及一个commondialog控件(右键工具箱--找到Microsoft commondialog 

control)

上一个:二进制转八进制VB代码
下一个:VB怎样编写浏览器

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,