Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a Browse To File dialog to a VB.NET application

Tags:

file-io

vb.net

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?

like image 362
David Avatar asked Jul 19 '10 17:07

David


People also ask

What is OpenFileDialog VB?

The OpenFileDialog control prompts the user to open a file and allows the user to select a file to open. The user can check if the file exists and then open it. The OpenFileDialog control class inherits from the abstract class FileDialog.

How do I open a dialogue file?

OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System.

Which function is used for opening the FileDialog box?

The Open dialog box lets the user specify the drive, directory, and the name of a file or set of files to open. You create and display an Open dialog box by initializing an OPENFILENAME structure and passing the structure to the GetOpenFileName function.


2 Answers

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.

like image 194
Sebastian Avatar answered Oct 07 '22 06:10

Sebastian


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 
like image 40
SLaks Avatar answered Oct 07 '22 06:10

SLaks