Sunday, July 8, 2012

c# - Count files in a Directory C#

string sInputFolderPath = ConfigurationSettings.AppSettings["INPUT_FOLDER_PATH"];
string sOutputFilePath = ConfigurationSettings.AppSettings["OUTPUT_FILE_PATH"];
try
{
     DirectoryInfo oDirectoryInfo = new DirectoryInfo(sInputFolderPath);
     using (StreamWriter oStreamWriter = new StreamWriter(sOutputFilePath))
     {
          foreach (DirectoryInfo oDirectory in oDirectoryInfo.GetDirectories())
          {
                Console.WriteLine("Directory Name: {0} Total Files: {1}", oDirectory.Name, oDirectory.GetFiles().Length);
                        oStreamWriter.WriteLine(@"{0}\t{1}", oDirectory.Name, oDirectory.GetFiles().Length);
          }
     }
}
catch (Exception ex)
{
      Console.WriteLine(ex.Message);
}
Console.WriteLine("Done");
Console.ReadLine();
}

No comments: