如何查询具有指定特性或名称的文件 (C#)

cs 复制代码
class FindFileByExtension  
{  
    // This query will produce the full path for all .txt files  
    // under the specified folder including subfolders.  
    // It orders the list according to the file name.  
    static void Main()  
    {  
        string startFolder = @"c:\program files\Microsoft Visual Studio 9.0\";  
  
        // Take a snapshot of the file system.  
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);  
  
        // This method assumes that the application has discovery permissions  
        // for all folders under the specified path.  
        IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);  
  
        //Create the query  
        IEnumerable<System.IO.FileInfo> fileQuery =  
            from file in fileList  
            where file.Extension == ".txt"  
            orderby file.Name  
            select file;  
  
        //Execute the query. This might write out a lot of files!  
        foreach (System.IO.FileInfo fi in fileQuery)  
        {  
            Console.WriteLine(fi.FullName);  
        }  
  
        // Create and execute a new query by using the previous
        // query as a starting point. fileQuery is not
        // executed again until the call to Last()  
        var newestFile =  
            (from file in fileQuery  
             orderby file.CreationTime  
             select new { file.FullName, file.CreationTime })  
            .Last();  
  
        Console.WriteLine("\r\nThe newest .txt file is {0}. Creation time: {1}",  
            newestFile.FullName, newestFile.CreationTime);  
  
        // Keep the console window open in debug mode.  
        Console.WriteLine("Press any key to exit");  
        Console.ReadKey();  
    }  
}
相关推荐
小浣熊熊熊熊熊熊熊丶3 小时前
飞牛NAS 安装 Teslamate 教程(docker版)
1024程序员节
程高兴3 小时前
模糊PID控制的永磁同步电机矢量控制系统-SIMULINK
matlab·1024程序员节
海海不瞌睡(捏捏王子)7 小时前
Unity知识点概要
unity·1024程序员节
unable code8 小时前
[网鼎杯 2020 玄武组]SSRFMe
网络安全·web·ctf·1024程序员节
开开心心就好2 天前
安卓免费证件照制作软件,无广告弹窗
linux·运维·安全·pdf·迭代器模式·依赖倒置原则·1024程序员节
开开心心就好7 天前
轻量级PDF阅读器,仅几M大小打开秒开
linux·运维·服务器·安全·pdf·1024程序员节·oneflow
学传打活11 天前
【边打字.边学昆仑正义文化】_12_生命价值的体现(1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
开开心心就好12 天前
小巧绿色免费关机工具,支持定时倒计时
linux·运维·服务器·安全·powerpoint·1024程序员节·foxmail
开开心心就好13 天前
跨平台高速下载工具,支持浏览器功能强大
运维·服务器·windows·pdf·旅游·媒体·1024程序员节
开开心心就好1 个月前
内存清理软件灵活设置,自动阈值快捷键清
运维·服务器·windows·pdf·harmonyos·risc-v·1024程序员节