Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 213063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:14:38+00:00 2026-05-11T18:14:38+00:00

I’m very new to C#. My boss asked me to wrote some code using

  • 0

I’m very new to C#. My boss asked me to wrote some code using listview as a file browser. I tried it and it seems it works. This code is to open the files from your drives and display it on the listView. It’s very simple. I also made an additional function where one can clear the displayed items in the listView. I would like to add additional feature, where I can also open the directory and not only the files.
By the way, here is the sample of my code:

    private void btnOpen_Click(object sender, EventArgs e)
    {
        string strSelectedPath;

        folderBrowserDialog1.ShowDialog();
        strSelectedPath = folderBrowserDialog1.SelectedPath;

        label1.Text = strSelectedPath;



        DirectoryInfo di = new DirectoryInfo(strSelectedPath);
        FileInfo[] files = di.GetFiles();

        foreach (FileInfo file in files)
        {
            listView1.Items.Add(file.Name);
        }
    }

    private void btnClear_Click(object sender, EventArgs e)
    {

        listView1.Items.Clear();
        label1.Text="";
    }

Could you please show me how?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:14:38+00:00Added an answer on May 11, 2026 at 6:14 pm

    If I understand your question correctly (you want to list not only the files in the selected directory, but also the sub-directories), you will want to look into the GetDirectories method of the DirectoryInfo class.

    You could do something like this, perhaps:

    DirectoryInfo di = new DirectoryInfo(strSelectedPath);
    // first list sub-directories
    DirectoryInfo[] dirs = di.GetDirectories();
    foreach (DirectoryInfo dir in dirs)
    {
        listView1.Items.Add(dir.Name);
    }
    // then list the files
    FileInfo[] files = di.GetFiles();
    foreach (FileInfo file in files)
    {
        listView1.Items.Add(file.Name);
    }
    

    Update: I would suggest that you move the above code into a separate method that takes a string parameter for the path (something like ListDirectoryContents(string path)). In this method you can start with clearing the items from the list view, setting the label text and then adding new content as above.

    This method can be called from your btnOpen_Click method, passing folderBrowserDialog1.SelectedPath in the path parameter.

    I usually try to keep my event handlers as small as possible, preferably not performing any real work but rather just call some other method that does the work. This opens up a bit more for triggering the same functionality from other places.

    Say for instance that your application can take a path as a command line parameter, then it will be cleaner code just calling ListDirectoryContents with the path from the command line than perhaps duplicating the same behaviour in your form.

    Full code example of that approach:

    private void btnOpen_Click(object sender, EventArgs e)
    {
        string path = BrowseForFolder();
        if (path != string.Empty)
        {
            ListDirectoryContent(path);
        }
    }
    
    private string BrowseForFolder()
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog() == DialogResult.OK)
        {
            return fbd.SelectedPath;
        }
        return string.Empty;
    }
    
    private void ListDirectoryContent(string path)
    {
        label1.Text = path;
        listView1.Items.Clear();
        DirectoryInfo di = new DirectoryInfo(path);
        // first list sub-directories
        DirectoryInfo[] dirs = di.GetDirectories();
        foreach (DirectoryInfo dir in dirs)
        {
            listView1.Items.Add(dir.Name);
        }
        // then list the files
        FileInfo[] files = di.GetFiles();
        foreach (FileInfo file in files)
        {
            listView1.Items.Add(file.Name);
        }
    
    }
    

    The upsides of this code is that you can reuse the method BrowseForFolder whenever you need to browse for a folder, since it simply returns the selected path and is not connected to what the path will be used for. Similarly, the ListDirectoryContent method is completely unaware of from where the path parameter comes; it may come from the folder browser, it may come from the command line or anywhere else.

    If you focus on having your methods performing only their “core task”, relying on input parameters for any additional information that they need you will get code that is easier to reuse and maintain.

    When it comes to event handlers (such as btnOpen_Click), I like to see them as triggers; they trigger things to happen, but the don’t really do a lot themselves.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I haven't seen any information on the net regarding how… May 12, 2026 at 12:37 am
  • Editorial Team
    Editorial Team added an answer w:Anders Hejlsberg: In 1996, Hejlsberg left Borland and joined archrival… May 12, 2026 at 12:37 am
  • Editorial Team
    Editorial Team added an answer Given your previous question, you're generating this string as part… May 12, 2026 at 12:37 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.