How to Search Folder or File under a Directory using Wildcard in C# VB.Net

Sometimes we need to search folders as well as files for a target location. .Net provides us System.IO namespace for file processing & managing purposes. By using GetDirectories we can search directories by wildcard and by using GetFiles we can search files by wildcard. Here in this small article I will describe both “How to search Directory” & “How to search Files”.

To do the Example first add 2 Label Control in your form. Then write the below code under Form_Load event:
C# Code:

        private void Form1_Load(object sender, EventArgs e)
            // Read Directories using WildCard
            string[] sDirectories = Directory.GetDirectories(@"C:\Codedisplay\Articles\", "Javascript_*", SearchOption.AllDirectories);
            foreach (string oDirectory in sDirectories)

            // Read Files using WildCard
            string[] sFiles = Directory.GetFiles(@"C:\Codedisplay\Articles\", "JQuery*", SearchOption.AllDirectories);
            foreach (string sFile in sFiles)
                label2.Text+=sFile + "\n";

Note: Don’t forget to add “System.IO” namespace.

VB.Net Code:

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ' Read Directories using WildCard
        Dim sDirectories As String() = Directory.GetDirectories("C:\Codedisplay\Articles\", "Javascript_*", SearchOption.AllDirectories)
        For Each oDirectory As String In sDirectories
            Label1.Text += oDirectory & vbLf

        ' Read Files using WildCard
        Dim sFiles As String() = Directory.GetFiles("C:\Codedisplay\Articles\", "JQuery*", SearchOption.AllDirectories)
        For Each sFile As String In sFiles
            Label2.Text += sFile & vbLf

    End Sub

Note: Don’t forget to add “System.IO” namespace.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code