3

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 4

4

Try the following code

Dim dialog As New OpenFileDialog()
If DialogResult.OK = dialog.ShowDialog Then
  TextBox1.Text = dialog.FileName
End If
Sign up to request clarification or add additional context in comments.

3 Comments

Argh. Reverse order of comparison in the If? In VB?!
@Konrad, it's an almost unbreakable habit from my C days.
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.
1

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

0

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

Hope this helps

Comments

0

Add a OpenFileDialog And Add This Code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.