none
How can I make that if I type in textBox2 something like ALL or * it will search any extension all extensions ? RRS feed

  • Question

  • string ext = Path.GetExtension(file);
    if (!files.Contains(file) && ext == "." + textBox2.Text)
                        {
                            files.Add(file);
                        }

    Now I can type a extension for example cs or txt and then later it will search over the files List.

    But how can I make that if I type in the textBox2 ALL or * or something that it will get any extension all the extension and will add the files ? 

    Wednesday, April 3, 2019 3:56 AM

Answers

  • Like this?

    string ext = Path.GetExtension(file);
    if (!files.Contains(file) && (ext == "." + textBox2.Text || textBox2.Text == "*"))
                        {
                            files.Add(file);
                        }

    • Marked as answer by Bialiko Wednesday, April 3, 2019 6:37 AM
    Wednesday, April 3, 2019 4:02 AM

All replies

  • Like this?

    string ext = Path.GetExtension(file);
    if (!files.Contains(file) && (ext == "." + textBox2.Text || textBox2.Text == "*"))
                        {
                            files.Add(file);
                        }

    • Marked as answer by Bialiko Wednesday, April 3, 2019 6:37 AM
    Wednesday, April 3, 2019 4:02 AM
  • Hi Bialiko,

    Thank you for posting here.

    For your question, you could try the code below.

     string[] files = Directory.GetFiles(textBox1.Text, "*.*", SearchOption.AllDirectories);         
                StringCollection col = new StringCollection();
    
                foreach (var file in files)
                {
                    string ext = Path.GetExtension(file);
                    if (textBox2.Text == "All" || textBox2.Text == "*")
                    {
                        col.Add(file);
                    }
                    else if (textBox2.Text.Contains(ext))
                    {
                        col.Add(file);
                    }
                }

    You could type the directory in textBox1 and type extension in textBox2. When you type .txt in textBox2, you could get all the files with extension .txt in col. When you type All, you would get all files in col.

    Best Regards,

    Wendy


    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.

    Wednesday, April 3, 2019 6:54 AM
    Moderator