开发者

How can I make a "browse for file.." button in VB.net?

I'm trying to build a simple FTP uploader. 开发者_开发知识库How can I make it so the user can select a file to upload? See, what I want is to have a button (which I do) that a user can click, and it shows the OpenFileDialog (which I have), but then when they select a file, I want its path to be shown in a text box. How can I do that?


Try the following code

Dim dialog As New OpenFileDialog()
If DialogResult.OK = dialog.ShowDialog Then
  TextBox1.Text = dialog.FileName
End If


One way is to convert the filename to a FileInfo which contains all sorts of information about the file including the path. This opens the dialog and displays the path of the selected file.

If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    Dim fi As New System.IO.FileInfo(OpenFileDialog1.FileName)
    TextBox1.Text = fi.DirectoryName
End If


You want to get the OpenFileDialog's property Filename property. See OpenFileDialog's class members here on MSDN.

Hope this helps


Add a OpenFileDialog And Add This Code

If YourOpenFileDialogName.ShowDialog = YourOpenFileDialogName.OK Then 
 textBox1.Text = YourOpenFileDialogName.FileName
End If
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜