tags:

views:

32

answers:

2

In a VB.NET Windows Forms application how do I add the capability for someone to click a button or image and open a file browser to browse to a file and assign it's path to a variable so I can copy that file to another specific path?

+2  A: 

You're looking for the OpenFileDialog class.

For example:

Sub SomeButton_Click(sender As Object, e As EventArgs) Handles SomeButton.Click
    Using dialog As New OpenFileDialog
        If dialog.ShowDialog() <> DialogResult.OK Then Return
        File.Copy(dialog.FileName, newPath)
    End Using
End Sub
SLaks
+1  A: 

You should use the OpenFileDialog class like this

Dim fd As OpenFileDialog = New OpenFileDialog() 
Dim strFileName As String

fd.Title = "Open File Dialog"
fd.InitialDirectory = "C:\"
fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
fd.FilterIndex = 2
fd.RestoreDirectory = True

If fd.ShowDialog() = DialogResult.OK Then
   strFileName = fd.FileName
End If

Then you can use the File class.

Sebastian
Thank you! What does RestoreDirectory = True do?
David
What does RestoreDirectory = True do?
David
if you open a dialog and choose a path, than you cancel the dialog. Next time you open the dialog, the first choosen path is shown again, if restoreDirectory is set to true. For detail information look at http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.restoredirectory.aspx
Sebastian