2016-11-20 59 views
0

我是新来C#的号码,我需要添加一个方法来已经是相当不错编译的程序的帮助。方法获取文件的路径和文件内它

我有以下代码:

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)); 
     } 

我必须保存到String变量目录​​的路径,我想那路径和计数的文件数量内的

请帮助我应该如何去写那个方法?

感谢帮助!

+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

您可以使用它导航到目录路径并获取文件数量。如果你想获得该目录中的所有文件系统项(其中包括其他

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

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

如果你只是想获得一个目录中的文件的数量,你可以这样做目录):

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