Using OpenFileDialog to select both folder and file.
Download Source
Download application
Introduction
Hi, guys as you all know that VB.net don't give you any built-in dialog which you can use in scenarios where you want to select or fetch both files and folders in a single showDialog() at the click of a button.Now, this has been a dream for many of the programmers who use VB.NET in most of their projects.Me too stumbled upon such a situation where in my application I needed my application to fetch the path of both a file and a folder using a single showDailog at the click of a button and as you guys know that VB.Net provides different dialogs for both of these.So after putting a lot of research and thinking into it I came up with an effective workaround which allows us to select both file and a folder using OpenFileDialog.
Logic
Now the basic logic that I applied is to turn the name validation of OpenFileDialog to false then turn file exists to false by default then turn path exists to true and then set a special keyword as the default file selection name.Using the Cod
I designed a simple program(while testing) which I am gonna use as an example here with only two controls a textbox and a button.Now at the click of the button, it will bring up the conventional OpenFileDialog box and will let us select a file or folder from our system and at the click of the open button it will display the path of the folder or file that we selected in the textbox.
Now the important block of code in our trick is as follows:
OpenFileDialog1.ValidateNames = False
OpenFileDialog1.CheckFileExists = False
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.FileName = "folderselection"
OpenFileDialog1.ShowDialog()
TextBox1.Text = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)
Now, this will help us fetch the path of a folder but there would be the keyword "folderselection" in front of that path as the default file selection name as you guys know that OpenFileDialog will not allow you to select a folder without selecting a file.Here note that I have used folderselection but you can use any other word or character or even digit as your default file name.Now, this may be a bit annoying if you intend to use that file path as a user selection and if you intend to work on that folder alone leaving the contents inside untouched.So for this, I applied another trick which will help us fetch only and only the path of whatever folder that we select without any default file name in front of it.
I added this single line to my code to my program.
TextBox1.Text = TextBox1.Text.Replace("folderselection", "").Trim()
Now what this does is that it excludes the word "folderselection" from whatever the path that gets fetched from the OpenFileDialog.Thereby giving us the path of the exact selection that we make whether it be a folder or a file.My final button click event would look like this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear() 'This is not necessary as I have added it to keep my textbox blank on each button click
OpenFileDialog1.ValidateNames = False OpenFileDialog1.CheckFileExists = False
OpenFileDialog1.CheckPathExists = True OpenFileDialog1.FileName = "folderselection" OpenFileDialog1.ShowDialog() TextBox1.Text=System.IO.Path.GetFullPath(OpenFileDialog1.FileName) TextBox1.Text = TextBox1.Text.Replace("folderselection", "").Trim()
End Sub
I hope my article helped you and If you want to see live testing of how this works for a better explanation watch my Youtube Video. Still, if you have got any difficulties or doubts/queries then feel free to leave a comment below or mail us. You can also inbox us at our official Facebook page.
If you liked our article then please do subscribe to our page for more articles like this and do leave a comment below.
Also, follow us on all our social media handles from the follow us tab from the top right sidebar.
Comments
Post a Comment