none
Folder browsing RRS feed

  • Question

  • Hi,

    I want to browse folders using folderbrowserdialog and filter only .csv, .xls, .xlsx files inside the folder.

    and display all the files in the listbox.( selecting multi folders ).

    and if i press reset button everything should be back to default showing no files in list box and allowing the user to browse the folder again.


    Akshay


    Thursday, April 25, 2019 5:45 AM

Answers

  • Hi

    Thank you for posting here.

    Based on your description, you want to browse folders by using folderbrowserdialog and filter only .csv, .xls, .xlsx files inside the folder.

    You could try the following code.

    private void Button3_Click(object sender, EventArgs e)
    
            {
    
                listBox1.Items.Clear();
    
            }
    
            private void Button1_Click(object sender, EventArgs e)
    
            {
    
                DialogResult result = folderBrowserDialog1.ShowDialog();
    
                if (result == DialogResult.OK)
    
                {
    
                    string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
    
                    foreach (string item in files)
    
                    {
    
                        if(item.EndsWith(".csv")|| item.EndsWith(".xls")|| item.EndsWith(".xlsx"))
    
                        {
    
                            listBox1.Items.Add(item);
    
                        }
    
                    }
    
               
    
                }
    
            }


    Result:

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 25, 2019 7:01 AM
    Moderator

All replies

  • Hi

    Thank you for posting here.

    Based on your description, you want to browse folders by using folderbrowserdialog and filter only .csv, .xls, .xlsx files inside the folder.

    You could try the following code.

    private void Button3_Click(object sender, EventArgs e)
    
            {
    
                listBox1.Items.Clear();
    
            }
    
            private void Button1_Click(object sender, EventArgs e)
    
            {
    
                DialogResult result = folderBrowserDialog1.ShowDialog();
    
                if (result == DialogResult.OK)
    
                {
    
                    string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
    
                    foreach (string item in files)
    
                    {
    
                        if(item.EndsWith(".csv")|| item.EndsWith(".xls")|| item.EndsWith(".xlsx"))
    
                        {
    
                            listBox1.Items.Add(item);
    
                        }
    
                    }
    
               
    
                }
    
            }


    Result:

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 25, 2019 7:01 AM
    Moderator
  • Hi,

    I'm getting this error while i try to run the code even after using System.IO;

    The name 'folderBrowserDialog1' does not exist in the current context


    Akshay


    Thursday, April 25, 2019 7:30 AM
  • Hi

    Thanks for the feedback.

    This is a control, you could add it from toolbox.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 25, 2019 7:33 AM
    Moderator
  • Thanks a lot .It worked

    Akshay

    Thursday, April 25, 2019 7:59 AM