Windows Forms: How to make a File Browser in C#

This tutorial will show you how to make a File Browser in C# using ListView control, then extract the icon associated with a file

As you know a ListView control is a window that displays a collection of items. ListView controls provide several ways to arrange and display items and are much more flexible than simple ListBox control. We will create a File Browser allows you to access files include icons in your hard disk.

To play demo you should create a New Project, by selecting Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "FileBrowser" and then click OK

c# file browser

Open your windows form designer then drag ListView, TextBox, Label, Button from your visual toolbox to your Form and layout your File Browser as shown below.

file browser in c#

After completing the FORM design, we will add code to handle your File Browser

You should create a list to contain the file name when you select the file from OpenFileDialog

List<string> listFiles = new List<string>();

then add a click event handler to Open button

private void btnOpen_Click(object sender, EventArgs e)
    //Clear all items
    //Open folder browser dialog
    using (FolderBrowserDialog fbd = new FolderBrowserDialog() { Description = "Select your path." })
        if (fbd.ShowDialog() == DialogResult.OK)
            //Set path to textbox
            txtPath.Text = fbd.SelectedPath;
            foreach (string item in Directory.GetFiles(fbd.SelectedPath))
                //Add image to imagelist
                FileInfo fi = new FileInfo(item);
                listFiles.Add(fi.FullName);//Add file name to list
                //Add file name and image to listview
                listView.Items.Add(fi.Name, imageList.Images.Count - 1);

Click the ListView control->select event SelectedIndexChanged then add your code as shown below

private void listView_SelectedIndexChanged(object sender, EventArgs e)
    if (listView.FocusedItem != null)
        Process.Start(listFiles[listView.FocusedItem.Index]); //Open process

You need to check FocusedItem of your listview before calling the Start method

The Process.Start method allows you to start a process resource and associates it with a process component.



Related Posts