You can use the DirectoryInfo and FileInfo Class to get all the Files.
Here is a sample code to achieve this:
Code:
Code Snippet
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace GetAllFiles
{
class Program
{
static void Main(string[] args)
{
//Counter for the Number of Files
int Count = 0;
//Directory to be Searched
string dir = @"C:\GetFiles";
DirectoryInfo dirInfo = new DirectoryInfo(dir);
//Get the Files in the Current Directory
FileInfo[] fInfo = dirInfo.GetFiles();
foreach (FileInfo f in fInfo)
{
Count++;
Console.WriteLine(f.FullName);
}
//Get the Files in the SubDirectories
Program p = new Program();
p.GetDirFiles(dirInfo, ref Count);
Console.WriteLine("Total Number of Files:" + Count);
}
private void GetDirFiles(DirectoryInfo dInfo, ref int Count)
{
DirectoryInfo[] subDirInfo = dInfo.GetDirectories();
foreach (DirectoryInfo sDir in subDirInfo)
{
FileInfo[] fSubInfo = sDir.GetFiles();
foreach (FileInfo f in fSubInfo)
{
Count++;
Console.WriteLine(f.FullName);
}
GetDirFiles(sDir,ref Count);
}
}
}
}
Hope it helps.
Bookmarks