- Published on : 15 December 2013 at 19:25 UTC
- Lionel Eppe
It is always interesting to allow a user to browse its hard drive when should specify the pathname of a file.
For this is simple, just use the component "FolderBrowserDialog" provided in ".Net Framework".
For this tutorial, you will need :
1) a "TextBox" with such property :
- Name: TB_folder_path
4) a "Button" with such property :
- Name: BT_browse
- Text : Browse
5) a "FolderBrowserDialog" with such property :
- Name : FolderBrowserDialog1 (default name)
The component "FolderBrowserDialog" allows you to customize it by changing the description to display in the window, change the default folder to display, ...
- Change the description to display in the window
- Change the default folder
- Display or not the "New Folder" button
- Change the root folder of the browser window
- Get the path of the selected folder
- Download the source (French version)
To change the text displayed at the top of the folder selection window, you simply change the property "Description".
FolderBrowserDialog1.Description = "Please select the folder where you want to save the downloaded files"
To change the default folder, you must change the property "SelectedPath".
' Select the "My Documents" folder by default FolderBrowserDialog1.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments ' You can also use a personal way by indicating as follows : FolderBrowserDialog1.SelectedPath = "C:\Users\InformatiWeb\Documents"
You can show or hide the "New Folder" button at the bottom of the window by changing the property "ShowNewFolderButton".
' Displays the "New Folder" button FolderBrowserDialog1.ShowNewFolderButton = True ' Hides the "New Folder" button FolderBrowserDialog1.ShowNewFolderButton = False
This feature can be confused with that to change the default folder. In fact, this feature allows the display to the user subfolders, sub-subfolders, ... a folder. In summary, if you set the "My Documents" folder as the root folder, the user will have the possibility to select a folder or sub folder in the "My Documents" folder. For example, the file "d:\my websites" will not be accessible in the event that we mentioned earlier.
You can not specify a path as you normally would. As your IDE (Visual Basic Express / Visual Studio) will tell you, the value will be a constant in this enumeration "Environment.SpecialFolder".
The example below shows you how to set the "My Documents" folder as the root folder :
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal
To reset this property, you must set it to "Desktop" which is the default.
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop
To retrieve the path of the selected folder, simply retrieve the value of the property "SelectedPath".
This property is actually two things :
- Define the default folder of the folder window or exploration
- Get the path selected by the user folder.
' It displays the form and tests whether the user has selected a file. ' The user will have clicked on the OK button. If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' Gets the full path of the file selected by the user Dim selected_folder As String = FolderBrowserDialog1.SelectedPath ' Displays the full path selected by the user in the case file (TextBox) TB_folder_path.Text = selected_folder ' Displays the folder name (only) selected, the user ' Subtlety, use the "IO.Path.GetFileName" function on the folder path ' to retrieve the name of the target folder. ' While "IO.Path.GetDirectoryName" display the path of the folder containing the target folder ' by the specified path parameter MsgBox("You have selected the file : " & IO.Path.GetFileName(selected_folder)) Else ' If the user has not selected a folder, we show a warning MsgBox("No file was selected", MsgBoxStyle.Exclamation, "No file selected") End If