Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

File Search By Extension in C#

 by Mayur Dighe on Sep 29, 2011

This blog intends to code the file search (of particular extension) program using C#.
Comments: 0 Views: 1046 Printable Version 

Description:

The article demonstrates how to search the files from the specified path of directory based on which type of file are you finding. This can be useful further if you want to code for finding the files from sub-directories.

Basic Requirements:
  • One TextBox Control
  • One ComboBox Control
  • One ListBox Control
  • Two Button Control

Procedure:

Step 1: Arrange the mentioned controls on windows form as follows.

windows form

Step 2: Write Code section for accessing path as String.

    private void btnFolder_Click(object sender, EventArgs e)
    {    
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.ShowDialog();
        textBox1.Text = fbd.SelectedPath.ToString();
    }

Step 3: Now, select Extension Type from ComboBox Control for Searching Files.

extension type in C#

Step 4: Now, Code to search the desired files from given path.
 
    private void btnSearch_Click(object sender, EventArgs e)
    {
        String path = textBox1.Text;
        string[] files = Directory.GetFiles(@path);
        bool search = false; //returns true if file(s) found

        for
(int i = 0; i < files.Length; i++)
        {
            if (String.Compare(Path.GetExtension(files[i]), comboBox1.Text , true) == 0)
            {
                search = true;
                listBox1.Items.Add(files[i]);
            }
        }

        if
(search == false)
        {
            listBox1.Items.Add("Search is Complete. There are no results to display.");
            label1.Text = "Total Files: 0";
        }
        else
        { 
            label1.Text = "Total Files: " + listBox1.Items.Count.ToString(); }
        }
    }

Step 5: Click the “Search” Button Control.

Step 6: If intended results found then the Name of that files are displayed in ListBox Control.

Intended Result:

file search in csharp

Summary:

In this article, we have seen how we to find the file names (of particular extension) from specified directory using C#.
 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor

 Blogger's Profile
Age: Not Available
Location:
Title: Student
Joined: Dec 26, 2010
Education: Not Available
 More Blogs from this Blogger
The Euclidean Algorithm
File Search By Extension in C#
Sql Server 2005 Vs Sql Server 2008
Scrolling Title in Windows Form
View all »
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.