2016-11-20 7 views
0

私はC#を初めて使用しています。既にかなりコンパイルされたプログラムにメソッドを追加する際に助けが必要です。ファイルパスとその中のファイル数を取得する方法

私は、次のコードを持っている:私はString変数に保存したディレクトリのパスを持っている、と私はそのパスを取得し、ファイルその内の数をカウントしたい

class Program 
    { 
     private static readonly ILog log = 
      LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
     static void Main(string[] args) 
     { 
      var startTimeOfApplication = DateTime.Now; 
      Helper.SetFileAppenderLogFile(); 
      Console.WriteLine("Started executing the MPL File processor at :: " + startTimeOfApplication); 
      log.Info("Started exection at :: " + DateTime.Now); 
      var filesPath = string.Empty; 
      if (args.Length > 0) 
      { 
       filesPath = args[0]; 
      } 
      else 
      { 
       filesPath = ConfigurationManager.AppSettings["mplFilesPath"].ToString(); 
      } 
      log.Info("MPL Files Path " + filesPath); 
      var mplFilesProcessor = new MPLFilesProcessor(); 
      mplFilesProcessor.ProcessMPLFiles(filesPath); 

      var endTimeOfApplication = DateTime.Now; 
      Console.WriteLine("Completed MPL Files Processor exection at :: " + endTimeOfApplication); 
      log.Info("Completed MPL Files Processor exection at :: " + endTimeOfApplication); 
      log.Info(string.Format("Total time taken to process is {0} minutes ", endTimeOfApplication.Subtract(startTimeOfApplication).TotalMinutes)); 
     } 

を。

どうすればこの方法を書くべきでしょうか?

ヘルプを評価してください!

+0

[フォルダからファイル数]の可能な重複(http://stackoverflow.com/questions/2242564/file-count-from-a-folder) –

+0

あなたが何をしようとしたことがありますか? – ItamarG3

+0

あなたの質問はコードなしで既にクリアされています。それは非常に混乱しているので、質問から完全に削除することを検討してください。 –

答えて

0

最も簡単な方法は、Directory.GetFiles()を呼び出してLengthプロパティを取得することです。

var path = @"C:\Windows"; 
var fileCount = Directory.GetFiles(path).Length; 
Console.WriteLine("{0} has {1} files in it", path, fileCount); 
0

これを使用して、ディレクトリパスに移動してファイル数を取得できます。

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\somedir"); 
int count = dir.GetFiles().Length; 
1

あなただけのディレクトリ内のファイルの数を取得したい場合は、あなたがこれを行うことができます:

int numberOfFiles = Directory.GetFiles(path).Count(); 

あなたが他の含まれている(そのディレクトリ内のすべてのファイルシステムエントリを取得したい場合ディレクトリ):

int numberOfFiles = Directory.GetFileSystemEntries(path).Count(); 
関連する問題