筛选文件类型C#

筛选文件类型C#,c#,C#,我正在编写一个控制台应用程序,以显示C:\windows上的文件夹统计信息,并显示其中的总文件。无论如何,它都可以简化,并将文件类型链接到用户!。到目前为止,我得到的是: { String extention = String.Empty; // Prompt the user to enter extention type Console.Wr

我正在编写一个控制台应用程序,以显示C:\windows上的文件夹统计信息,并显示其中的总文件。无论如何,它都可以简化,并将文件类型链接到用户!。到目前为止,我得到的是:

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

尝试使用:
newdirectoryinfo(“C:\\Windows”)

如果要获取该目录中的文件列表,请调用:

EnumerateFiles("*",SearchOption.AllDirectories) 
在DirectoryInfo对象上,查看以下内容:

“如何:获取有关文件、文件夹和驱动器的信息(C#编程指南)”

例如:

// 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更改为所需的格式

更新:

  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("*.*");

看看这个话题:嗨,我是一个还在学习的学生,我把firInfo改成了Info。。。但是没有任何链接到GetFiles,请您帮助,以便正确地声明阳刚之气请参阅我文章中的更新。(您的意思是如何访问.GetFiles()方法?)