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
2010-07-19 17:32:53
+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
2010-07-19 17:40:40
Thank you! What does RestoreDirectory = True do?
David
2010-07-19 19:36:25
What does RestoreDirectory = True do?
David
2010-07-19 19:41:57
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
2010-07-19 22:59:45