C# Visual Studio: Show files of a folder in listbox using FolderBrowseDialog — March 14, 2015

C# Visual Studio: Show files of a folder in listbox using FolderBrowseDialog

Hello friends,have you ever tried to all the files of a folder into the listbox! Maybe someday you need to do that in case,you want to play the videos in media player,or if you want to change the files’ name etc.

So,let’s see how to get started with it.1

– As you can see in the above image,the FolderBrowserDialog in the toolbox is highlighted.

– Drag it on the form.

– Drag the Button and the ListBox.Change the name of the button(if you want to) and enlarge the ListBox.

6

-Now Double Click on the button,to go to the button’s click event.

-Now paste the following code into the button1_click event i.e. private void button1_Click(Object sender,EventArgs e){


folderBrowserDialog1 = new FolderBrowserDialog(); //to implement a new folder browser

DialogResult result = folderBrowserDialog1.ShowDialog();  
if (result == DialogResult.OK)                              
{
String[] allFiles = System.IO.Directory.GetFiles(folderBrowserDialog1.SelectedPath);  //store the files in the string array

for(int i=0;i
{
listBox1.Items.Add(allFiles[i]);         // use the for loop to continously add the file addresses to the listbox
}
}


Untitled-3

Output

-Run the program and you will see the click on the Button and you will see the Dialog openes as shown in the image below.Select the folder and click OK.

Untitled

– You will see the file addresses added to the listbox as shown below.

Untitled-2