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?
4 Answers
Try the following code
Dim dialog As New OpenFileDialog()
If DialogResult.OK = dialog.ShowDialog Then
TextBox1.Text = dialog.FileName
End If
3 Comments
Konrad Rudolph
Argh. Reverse order of comparison in the
If? In VB?!JaredPar
@Konrad, it's an almost unbreakable habit from my C days.
Konrad Rudolph
I’m lucky then that I made the transition in the other direction. ;-) I just hope there aren’t too may BASIC idioms in my C++ code.
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
Comments
You want to get the OpenFileDialog's property Filename property. See OpenFileDialog's class members here on MSDN.
Hope this helps