views:

183

answers:

4

Hello. 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? Screenshot: http://i49.tinypic.com/oi9idg.jpg

A: 

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

Hope this helps, Best regards, Tom.

tommieb75
+3  A: 

Try the following code

Dim dialog As New OpenFileDialog()
If DialogResult.OK = dialog.ShowDialog Then
  TextBox1.Text = dialog.FileName
End If
JaredPar
Argh. Reverse order of comparison in the `If`? **In VB**?!
Konrad Rudolph
@Konrad, it's an almost unbreakable habit from my C days.
JaredPar
@Jared: 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.
Konrad Rudolph
+1  A: 

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
djbaldwin