程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> LINQ對文件目錄的基本操作

LINQ對文件目錄的基本操作

編輯:C#入門知識

RT,給學習LINQ的新手們一些借鑒。本文主要寫了6個文件操作,代碼分別貼上。
/*通過函數的形式表現出來*/

C# code

        //1、查詢指定名稱的文件
        private static void FileQuery()
        {
            ///獲取"我的文檔"的路徑
            string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            List<FileInfo> files = new List<FileInfo>();
            ///獲取文件列表信息
            foreach (var file in Directory.GetFiles(directory))
            {
                files.Add(new FileInfo(file));
            }
            ///查詢文件列表信息
            var filevalues = from file in files
                             where file.Extension == ".pdf" && file.Name.IndexOf("報告") > -1
                             orderby file.Name
                             select file;
            ///顯示查詢結果
            foreach (var f in filevalues)
            {
                Console.WriteLine(f.Name);
            }
        }
             //運行結果:
             /*
             個人述職報告.pdf
             個人述職報告-96.pdf
              WPF程序設計指南(有書).pdf
             */

 


C# code

        //2、查詢指定擴展名的文件
        private static void FileExtensionsQuery()
        {
            ///獲取"我的文檔"的路徑
            string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            List<FileInfo> files = new List<FileInfo>();
            ///獲取文件列表信息
            foreach (var file in Directory.GetFiles(directory))
            {
                files.Add(new FileInfo(file));
            }
            ///按文件擴展名分組
            var fExtensions = from fe in files
                              group fe by fe.Extension into g
                              orderby g.Key
                              select g;
            ///顯示查詢結果
            foreach (var f in fExtensions)
            {
                foreach (var fi in f)
                {
                    Console.WriteLine(fi.Name);
                }
            }
        }
            //運行結果:
             /*
            desktop.ini
            個人述職報告.pdf
            個人述職報告-96.pdf
             WPF程序設計指南(有書).pdf
            1.png
            個人述職報告.txt
             */

 


C# code

        //3、查詢指定屬性的文件
        private static void FileAttributeQuery()
        {
            ///獲取"我的文檔"的路徑
            string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ///獲取文件信息
            List<FileInfo> files = new List<FileInfo>();
            ///獲取文件列表信息
            foreach (var file in Directory.GetFiles(directory))
            {
                files.Add(new FileInfo(file));
            }
            //根據文件大小(大於1M小於4M)查詢文件,並進行分組排序,
            var fAttribute = from fa in files
                             where fa.Length >= 1024 * 1024 && fa.Length <= 4096 * 1024
                             orderby fa.Name
                             select fa;
            ///顯示查詢結果
            foreach (var fa in fAttribute)
            {
                Console.WriteLine(fa.Name);
            }
        }
//運行結果:
/*WPF程序設計指南(有書).pdf*/

 


C# code

        //4、統計文件夾的大小
        private static void FileSizeQuery()
        {
            ///獲取"我的文檔"的路徑
            string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ///獲取文件信息
            List<FileInfo> files = new List<FileInfo>();
            ///獲取文件列表信息 www.2cto.com
            foreach (var file in Directory.GetFiles(directory))
            {
                files.Add(new FileInfo(file));
            }
            //搜索文件的大小
            var fSize = from fs in files
                        select fs.Length;
            ///對文件大小求和
            long size = fSize.Sum();
            ///顯示查詢結果
            Console.WriteLine(size.ToString());
        }
//運行結果:
/* 10786712 */

 


C# code

        //5、比較兩個文件夾是否存在同名文件
        private static void FileCompareQuery()
        {
            ///獲取"我的文檔"的路徑
            string directoryA = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ///獲取"我的圖片"的路徑
            string directoryB = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            ///獲取我的文檔的文件信息
            List<FileInfo> filesA = new List<FileInfo>();
            ///獲取我的圖片的文件信息
            List<FileInfo> filesB = new List<FileInfo>();
            ///獲取我的文檔文件列表信息
            foreach (var file in Directory.GetFiles(directoryA))
            {
                filesA.Add(new FileInfo(file));
            }
            ///獲取我的圖片文件列表信息
            foreach (var file in Directory.GetFiles(directoryB))
            {
                filesB.Add(new FileInfo(file));
            }
            ///查詢同名文件
            var fSameFile = from fA in filesA
                            join fB in filesB on fA.Name equals fB.Name
                            orderby fA.Name
                            select fA;
            ///顯示查詢結果
            foreach (var fsf in fSameFile)
            {
                Console.WriteLine(fsf.Name);
            }
        }

運行結果:
/*  1.png  */

 


C# code

        //6、查詢包含指定文件內容的txt文檔
        private static void FileExtensionsTextQuery()
        {
            ///獲取"我的文檔"的路徑
            string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            List<FileInfo> files = new List<FileInfo>();
            ///獲取文件列表信息
            foreach (var file in Directory.GetFiles(directory))
            {
                files.Add(new FileInfo(file));
            }
            string FindString = "我相信自己";
            ///按文件擴展名分組
            var fExtensionsText = from fetq in files
                                  where fetq.Extension == ".txt"
                                  let content = File.ReadAllText(fetq.FullName, System.Text.Encoding.GetEncoding("gb2312"))
                                  where content.Contains(FindString) == true
                                  orderby fetq.Name
                                  select fetq;
            ///顯示查詢結果
            foreach (var f in fExtensionsText)
            {
                Console.WriteLine(f.Name);
            }
        }
           //運行結果:
            /*
             個人述職報告.txt
           */

 

總結:6個文件的操作整體上比較簡單,主要是結合LINQ來操作。希望共同學習。



摘自 Chinajiyong

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved