Join Us!

Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.
A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Login

Enter your Project Envision username.
Enter the password that accompanies your username.
Request new password

List files in a directory using C#

.NET comes with many classes that make accomplishing almost any task quite easy. In below program I will present how easy is to retrieve a list of files in a directory using DirectoryInfo and FileInfo classes.

Here is full code of example program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ListFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("You have to provide directory");
                Console.WriteLine(@"Example ListFiles.exe c:\test");
                Console.WriteLine("To return only specific file types use second argument");
                Console.WriteLine(@"Example ListFiles.exe c:\test txt");
            }
            else
            {
                try
                {
                    Console.WriteLine(args[0]);
                    DirectoryInfo di = new DirectoryInfo(args[0]);
                    FileInfo[] files = null;
                    if (args.Length == 1) {
                        files = di.GetFiles();
                    } else if (args.Length == 2)  {
                        files = di.GetFiles("*." + args[1]);
                    }
                    if (files != null && files.Length > 0)
                    {
                        foreach (FileInfo file in files)
                        {
                            Console.WriteLine(file.Name + " " + file.CreationTime);
                        }
                    }
                    else
                    {
                        Console.WriteLine("No files");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occured");
                    Console.WriteLine(ex.Message);
                    Console.WriteLine();
                }
            }

            Console.ReadLine();
        }
    }
}

To List all files in a directory you use:

ListFiles.exe c:\books

To List all pdfs in a directory you use:

ListFiles.exe c:\books pdf

The classes required to list files in directory are DirectoryInfo and FileInfo, which are included in System.IO namespace, which was aded to our code like this:

using System.IO;

Below piece of code checks if we supplied command at least one command line argument. If user won't provide any parameter Example usage text will be printed out.

if (args.Length < 1)
            {
                Console.WriteLine("You have to provide directory");
                Console.WriteLine(@"Example ListFiles.exe c:\test");
                Console.WriteLine("To return only specific file types use second argument");
                Console.WriteLine(@"Example ListFiles.exe c:\test txt");
            }

If user provided at least one argument, we create new DirectoryInfo using first parameter as a path to direcotry and we declare empty FileInfo array to store information about files from that direcotry.

    DirectoryInfo di = new DirectoryInfo(args[0]);
    FileInfo[] files = null;

To get list off files in direcotry we use one of two versions of GetFiles method of DirectoryInfo.
The first version does not accept any arguments and returs all the files, the second accepts one argument, which is extension in form i.e. "*.pdf"

if (args.Length == 1) {
    files = di.GetFiles();
} else if (args.Length == 2)  {
    files = di.GetFiles("*." + args[1]);
}

The last step is just go through all the returned files using foreach array.

You can download the source code (solution in Visual Studio 2008 Express) here

Categories: