Wednesday, January 30, 2013

C# File in Directory List

To Get List of File in directory. Sample Code:
 
//include this
using System.IO;

//set directory to scan
var dirInfo = new DirectoryInfo(@"C:\FolderName");

//checking if folder exists
if (dirInfo.Exists) {
    //*.* = get all file name
    FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly);   

    foreach (FileInfo file in files) {
        //example to list the file name using messagebox
        MessageBox.Show(file.ToString());
    }
}
Additional Reading at :
  1. MSDN DirectoryInfo
  2. MSDN GetFiles

No comments:

Post a Comment