How to Create a Simple File Manager Application in Windows Forms

It can be very beneficial to recreate common applications when learning Windows Forms. It can also help reinforce your basic understanding of programming. An example application you can create is a basic file manager.

The file manager application should include quick links to common Windows folders (Desktop, My Documents, Pictures, Music, and Videos). It should also have the ability to navigate directories up and down.

USE VIDEO OF THE DAY

How to add UI elements on the canvas

Start by creating a new Windows Form application in Visual Studio. Once done, you can start adding UI elements for the file manager to the canvas.

  1. With the canvas open, search for a Button UI element using toolbox.
  2. Drag and drop five buttons to represent the quick links available on a Windows computer. These will represent the Desktop, My Documents, Pictures, Music, and Videos folders.
  3. Highlight each button in turn and navigate to the Properties window. Change each button Property name and Text property to the following values:
    Button Property name Text property
    1 button_Desktop Desk
    2 button_Documents My Documents
    3 button_Pictures Pictures
    4 button_Music Music
    5 video_button Videos
  4. Find a FlowLayoutPanelFlowLayoutPanel UI element using toolbox.

  5. Drag and drop it onto the canvas and place it next to the quick links. This is where you will display your files and directories based on the directory you are currently viewing.
  6. Highlight the panel and change the Last name property to “panel_FilesList”.


How to get current file path

When you click a button (for example, Desktop), the list of folders and files that are on the Desktop should appear in the panel. To do this, you can get the file path of the folder you clicked on, i.e. “C:UsersUserDesktop”.

Write the logic for this in the C# code-behind file for the form. If you are unfamiliar with C# classes, you can read more about Classes in C#.

  1. Double-click each of the user interface buttons. This will generate click event functions for each button in the form’s C# file. These functions will run when you click the associated button.
    private void button_Desktop_Click(object sender, EventArgs e)
    {
    }

    private void button_Documents_Click(object sender, EventArgs e)
    {
    }

    private void button_Pictures_Click(object sender, EventArgs e)
    {
    }

    private void button_Music_Click(object sender, EventArgs e)
    {
    }

    private void button_Videos_Click(object sender, EventArgs e)
    {
    }

  2. To get the file path of a special Windows directory such as the desktop, use the Environment.GetFolderPath() method. This will return a string of the file path for the folder, for example, “C:UsersUserDesktop”. Add this to each function and pass the filepath value into another function called ShowFiles()that you will create later.
    public Form1()
    {
    InitializeComponent();
    DisplayFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
    }

    private void button_Desktop_Click(object sender, EventArgs e)
    {
    DisplayFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
    }

    private void button_Documents_Click(object sender, EventArgs e)
    {
    DisplayFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
    }

    private void button_Pictures_Click(object sender, EventArgs e)
    {
    DisplayFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
    }

    private void button_Music_Click(object sender, EventArgs e)
    {
    DisplayFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
    }

    private void button_Videos_Click(object sender, EventArgs e)
    {
    DisplayFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos));
    }


How to render files for selected directory

Display a list of files based on the Desktop, My Documents, Pictures, Music, or Videos button you click.

  1. Import the System.IO namespace at the top of the file.
    using System.IO;
  2. Create a new function called ShowFiles()to display files and folders in a selected directory.
    private void DisplayFiles(string filePath)
    {
    }
  3. Inside the new ShowFiles() compile a list of all the directories and files that are in the selected folder.
    string[] filesList = Directory.GetDirectories(filePath).Concat(Directory.GetFiles(filePath)).ToArray();
  4. Make sure the panel that displays the list of files and directories is empty.
    panel_FilesList.Controls.Clear();
  5. Store the current directory in a global variable.
    currentLocation = filePath;
  6. Declare the global variable at the top of the class.
    stringcurrentLocation = "";
  7. Back in the ShowFiles() function, create a button for each directory and file. In a for loop, isolate the name from the rest of the file path. Programmatically create a new button, with the name of the file or directory as the button text.
    for (int i = 0; i < filesList.Length-1; i++)
    {
    bool isHidden = ((File.GetAttributes(filesList[i]) & FileAttributes.Hidden) == FileAttributes.Hidden);

    if (!isHidden)
    {
    var startOfName = filesList[i].LastIndexOf("");
    var fileName = filesList[i].Substring(startOfName + 1, filesList[i].Length - (startOfName + 1));


    Button newButton = new Button();
    newButton.Text = fileName;
    newButton.Name = filesList[i];
    newButton.Location = new Point(70, 70);
    newButton.Size = new Size(800, 100);
    newButton.TextAlign = ContentAlignment.MiddleLeft;
    newButton.Padding = new Padding(24, 0, 0, 0);
    panel_FilesList.Controls.Add(newButton);
    }
    }

  8. Press the play button at the top of Visual Studio to run the program.
  9. Click on one of the quick links. For example, if you click My DocumentsThe right panel should contain a list of files and folders in the My Documents case.
  10. If you click another quick link, for example, Videosthe panel should fade out and change to show the files and folders in the Videos case.


How to Navigate the Up and Down Directories

Modify the code so that when you click on a folder displayed in the list, you go down one directory. You can also create a back button to return to the previous folder.

  1. To drill down into a directory, create a new function called button_Click_Open(). If the user clicks on a directory in the list, the program will call the ShowFiles() function to display the files inside this directory. If the user clicks on a file instead, the program will open the file.
    private void button_Click_Open(object sender, EventArgs e)
    {
    Button button = (Button)sender;
    string filePath = button.Name;

    try
    {
    DisplayFiles(filePath);
    }
    catch (Exception ex)
    {
    var process = new System.Diagnostics.Process();
    process.StartInfo = new System.Diagnostics.ProcessStartInfo() { UseShellExecute = true, FileName = filePath };
    process.Start();
    }
    }

  2. In the ShowFiles() function, modify the part of the code that creates a new button. Add a new line after creating the button to attach the new one button_Click_Open() function at the Click event.
    newButton.Click += button_Click_Open;
  3. To go up in a directory, create a return button. On the canvas, click and drag a new button using the toolbox.
  4. Highlight the new Return button. In the properties window, change the Text property on “
  • Double click on the Return button to generate a click event function that will fire when the user clicks the back button.
    private void back_Click(object sender, EventArgs e)
    {
    }
  • In the function, find the path to the folder at the top level and view the files and directories in the folder above.
    var previousFolder = this.currentLocation.Substring(0, this.currentLocation.LastIndexOf(""));
    DisplayFiles(previousFolder);
  • Press the play button at the top of Visual Studio again to run the program. Click any of the quick links again to display a list of folders or files.
  • In the panel, click on a folder to go down into a directory.
  • Click the back button to return to the previous directory.

  • Creating Desktop Applications Using Windows Forms

    A file manager is a simple application that you can create when you learn how to create a Windows Form application. You can use the toolbox to drag and drop a panel onto the canvas, as well as various other buttons.

    Now that your application has the core functionality, feel free to modify the button properties to improve the file manager design.

    Check out some of the other beginner apps you can build when you learn Windows Forms apps.

    Comments are closed.