ProgTalk - Your archive for all source code

Get all files of directory and sub directories using ASP.NET, C#, and VB.NET

ProgTalk » Articles » .NET, C#, ASP.NET, VB.NET » Get all files of directory and sub directories using ASP.NET, C#, and VB.NET
 
Author: Rockin J
Page Views: 27032
Average Article Rating: rating starrating starrating starrating starrating star
 

Recently I had to find all the files of a directory, and it’s sub directories.  I thought there would be an out of the box way to do this, but discovered there was no way.  After googling I discovered a few different sources and put together something of my own.  Here I am sharing how you can achieve this as well:

The first step is to call our namespace to easy coding:

C#:

 

 

using System.IO;

 


VB.NET

 

 

Imports System.IO

 

 

Next we will call create a function which passes in our base/root path.  Using this path we will check if there are any child folders, or if it is a file itself.  If it is a file, we will print out the file path.  If it is a directory, we will check if it has sub directories or any files within it, and print them out as well.  This is done through a recursive call.

 

C#

 

    public void GetFiles(string path)

    {

        if (File.Exists(path))

        {

            // This path is a file

            ProcessFile(path);

        }

        else if (Directory.Exists(path))

        {

            // This path is a directory

            ProcessDirectory(path);

        }

    }

   

    // Process all files in the directory passed in, recurse on any directories

    // that are found, and process the files they contain.

    public void ProcessDirectory(string targetDirectory)

    {

        // Process the list of files found in the directory.

        string[] fileEntries = Directory.GetFiles(targetDirectory);

        foreach (string fileName in fileEntries)

            ProcessFile(fileName);

 

        // Recurse into subdirectories of this directory.

        string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);

        foreach (string subdirectory in subdirectoryEntries)

            ProcessDirectory(subdirectory);

    }

 

    // Insert logic for processing found files here.

    public void ProcessFile(string path)

    {

        FileInfo fi = new FileInfo(path);

        Response.Write("File Number " + position.ToString() + ". Path: " + path + " <br />");

        position++;

    }

 

 

VB.NET

 

    Public Sub GetFiles(ByVal path As String)

        If File.Exists(path) Then

            ' This path is a file

            ProcessFile(path)

        ElseIf Directory.Exists(path) Then

            ' This path is a directory

            ProcessDirectory(path)

        End If

    End Sub

 

 

    ' Process all files in the directory passed in, recurse on any directories

    ' that are found, and process the files they contain.

    Public Sub ProcessDirectory(ByVal targetDirectory As String)

        ' Process the list of files found in the directory.

        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)

        For Each fileName As String In fileEntries

            ProcessFile(fileName)

        Next

 

        ' Recurse into subdirectories of this directory.

        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)

        For Each subdirectory As String In subdirectoryEntries

            ProcessDirectory(subdirectory)

        Next

    End Sub

 

    ' Insert logic for processing found files here.

    Public Sub ProcessFile(ByVal path As String)

        Dim fi As New FileInfo(path)

        Response.Write("File Number " + position.ToString() + ". Path: " + path + " <br />")

        position += 1

    End Sub


To use the methods just call the following method with our base/root directory:

 

C#

 

GetFiles("C:\\Test\\");

 



VB.NET

 

GetFiles("C:\Test\")

 


This helped solve some problems on my side.  Hope this helps you as well.

 

Notes

 

Tags

 

Ratings

Was this article helpful? Don't forget to rate it. Ratings helps community members identify top & useful articles.
Current Rating: rating starrating starrating starrating starrating star
Rate this article:
(1-Poor, 2-Needs Improvement, 3-Average, 4-Good, 5-Excellent)
 

Feedback

Have a question, suggestion or feedback for this article? Leave your comments below.
Share your feedback:
Need to upload images with your comment. Upload your images here: Image Shack. Remember to copy the url of your uploaded image and insert it into the comment.
You can insert images by clicking on the following icon: insert comment image
(Members Only)
 

Latest Comments

Below are the latest comments from other ProgTalk members.
Posted on:
8/25/2009 12:00:00 AM
Posted by:
thanks you very much chat sohbet odalari kamerali sohbet
Posted on:
5/6/2009 12:00:00 AM
Posted by:
Super Duper... Thank you VERY much. One of the most usefull programs i have ever downloaded :-)
Posted on:
3/5/2009 12:00:00 AM
Posted by:
Thank you very much.
Posted on:
3/5/2009 12:00:00 AM
Posted by:
Thank you very much.
Posted on:
2/24/2009 12:00:00 AM
Posted by:
just getting into ASP.NET from Coldfusion and my brain is num but this article worked great for my needs. Took a bit of tweaking at first but once I understood VS it worled great..cheers
Posted on:
1/29/2009 12:00:00 AM
Posted by:
Very nice thanks for the posting
Posted on:
12/10/2008 12:00:00 AM
Posted by:
Thank you for coding, its very useful to us. as well as i want see complete details about Directory And Sub Directory Information like (File name,path,size,Extension and LastAccessed). Then We''re store all details to the database.B''coz i''m new vb.net.... Please help me.... i''m using mysql

                                                                                                                                                                  Regards,

                                                                                                                                                             A.GomathiNayagam