2014-01-03 6 views
-5

Я пишу консольное приложение для отображения статистики папки на C: \ windows и показываю, что все файлы там есть, поэтому их можно упростить и связать тип файла с пользователем !. Это то, что я получил до сих пор:Типы файлов фильтрации C#

   { 
        String extention = String.Empty; 
        // Prompt the user to enter extention type 
        Console.Write("Please enter extention type: "); 
        extention = Console.ReadLine(); 

        // This gets the Folder location which in this case is C:\\windows 
        DirectoryInfo root = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows)); 

        // This is basicly the bit that collects the data after the user has entered the extention type 

        FileInfo[] executables = root.GetFiles("*exe"); 

        foreach (var exe in executables) 
        { 
         //This will show the word txt in the console window 
         Console.WriteLine(exe.Name); 
        } 
       } 
      } 
     } 
     { 
      String extention2 = String.Empty; 

      // Prompt the user to enter extention type 
      extention2 = Console.ReadLine(); 

      // This gets the Folder location which in this case is C:\\windows 
      DirectoryInfo root2 = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows)); 

      FileInfo[] text = root2.GetFiles("*.txt"); 

      foreach (var txt in text) 
      { 
       //This will show the word txt in the console window 
       Console.WriteLine(txt.Name); 
      } 
     } 

      String extention4 = String.Empty; 

      // Prompt the user to enter extention type 
      extention4 = Console.ReadLine(); 

      // This gets the Folder location which in this case is C:\\windows 
      DirectoryInfo root4 = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows)); 

      FileInfo[] windows = root4.GetFiles("*.win"); 

      foreach (var win in windows) 
      { 
       //This will show the word txt in the console window 
       Console.WriteLine(win.Name); 
      } 
+0

Посмотреть эту тему: http://stackoverflow.com/questions/20881645/how-to-display-the-statistics-about-all-the-files-in-a-folder-in-c- sharp/20881751 # 20881751 – Odrai

ответ

2

можно использовать: new DirectoryInfo("C:\\Windows")

Если вы хотите, чтобы получить список файлов в этом каталоге, то звоните по телефону:

EnumerateFiles("*",SearchOption.AllDirectories) 

на объект DirectoryInfo

1

Посмотрите на это:

«Как: Получить Информатио п о файлах, папки и диски (C#) Руководство по программированию»

http://msdn.microsoft.com/en-us/library/6yk7a1b0.aspx

Например:

// Get the files in the directory and print out some information about them. 
System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*"); 


foreach (System.IO.FileInfo fi in fileNames) 
{ 
     Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length); 
} 

Вы можете изменить Console.WriteLine в формат, который вы хотите ...

Update:

System.IO.DriveInfo di = new System.IO.DriveInfo(@"C:\"); 

    // Get the root directory 
    System.IO.DirectoryInfo dirInfo = di.RootDirectory; 

    // And then you can do .GetFiles() 
    System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*"); 
+0

Привет, я все еще участвую в учебе и исправляю фиксированную firInfo, меняя его на Info ... но нет ничего, связанного с GetFiles, можете ли вы помочь, чтобы мужество было правильно объявлено. – MattTheCodeMan

+0

См. Обновление в моем сообщении , (Вы имеете в виду, как вы можете получить доступ к методу .GetFiles()?) – Odrai

Смежные вопросы