乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      LINQ和文件目錄

       Coder編程 2020-04-16

      記錄https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/linq/linq-and-file-directories的學習

       

      查詢具有指定擴展名的文件 (SearchOption.AllDirectories 指遞歸文件夾獲取所有文件)

      string startFolder = @"C:\Users\bibi\Desktop\代碼\異步\ConsoleApp4\Test\";
      DirectoryInfo dir = new DirectoryInfo(startFolder);
      IEnumerable<FileInfo> fileList = dir.GetFiles("*.*", SearchOption.AllDirectories);
      
      var fileQuery = from file in fileList
                      where file.Extension == ".png"
                      orderby file.Name
                      select file;
      
      foreach(var item in fileQuery)
      {
          Console.WriteLine(item.FullName);
      }

      按擴展名對文件進行分組

      // Take a snapshot of the file system.  
      string startFolder = @"C:\Users\bibi\Desktop\代碼\異步\ConsoleApp4\Test\";
      
      // Used in WriteLine to trim output lines.  
      int trimLength = startFolder.Length;
      
      // 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);
      
      var queryGroupByExt = from file in fileList
                            group file by file.Extension.ToLower() into fileGroup
                            orderby fileGroup.Key
                            select fileGroup;
      
      foreach(var group in queryGroupByExt)
      {
          Console.WriteLine(group.Key);
          foreach(var item in group)
          {
              Console.WriteLine($"    {item.Name}");
          }
      }

      求目錄下的所有文件的總字節(jié)數(shù)

      string startFolder = @"C:\Users\bibi\Desktop\代碼\異步\ConsoleApp4\Test\";
      
      System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);
      var totalLength = dir.GetFiles().Sum(x=>x.Length);
      
      Console.WriteLine(totalLength+" bytes");

      對接文件夾中的文件(序列求等SequenceEqual、交集Insect、差集Except)

      string pathA = @"C:\Users\bibi\Desktop\代碼\異步\ConsoleApp4\Test\";
      string pathB = @"C:\Users\bibi\Desktop\代碼\異步\ConsoleApp4\Test\TestX\";
      
      System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
      System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);
      
      //使用了頂層目錄SearchOption.TopDirectoryOnly,不需遞歸目錄下的文件夾尋找文件,只要第一層文件。 IEnumerable
      <System.IO.FileInfo> list1 = dir1.GetFiles("*.*", SearchOption.TopDirectoryOnly); IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", SearchOption.TopDirectoryOnly); //使用自定義的文件默認比較器 FileCompare myFileCompare = new FileCompare(); //判斷兩個序列是否相等 bool isEqualOne = list1.SequenceEqual(list2, myFileCompare); if (isEqualOne == true) { Console.WriteLine("the two folders are the same"); } else { Console.WriteLine("The two folders are not the same"); } //求交集文件 var queryCommonFiles = list1.Intersect(list2, myFileCompare); if (queryCommonFiles.Any()) { Console.WriteLine("The following files are in both folders:"); foreach (var v in queryCommonFiles) { Console.WriteLine(v.FullName); //shows which items end up in result list } } //求差集文件 在list1卻不在list2的文件 var queryList1Only = list1.Except(list2, myFileCompare); Console.WriteLine("The following files are in list1 but not list2:"); foreach (var v in queryList1Only) { Console.WriteLine(v.FullName); }

       查找目錄中最大、最小的一個或多個文件。

      using System;
      using System.Collections.Generic;
      using System.Data;
      using System.Diagnostics.CodeAnalysis;
      using System.IO;
      using System.Linq;
      using System.Reflection;
      
      namespace ConsoleApp4
      {
          class Program
          {
              static void Main(string[] args)
              {
                  string startFolder = @"C:\Users\mycomputer\Desktop\代碼\異步\ConsoleApp4\Test\TestX\";
      
                  System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);
                 
                  IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
      
                  //查找文件最大字節(jié)數(shù)
                  long maxSize = fileList.Select(x => GetFileLength(x)).Max();
                 
                  Console.WriteLine($"The length of the largest file under {startFolder} is {maxSize}");
      
                  //查找字節(jié)數(shù)最大的文件
                  var longestFile = fileList.OrderByDescending(x => GetFileLength(x)).First();          
      
                  Console.WriteLine("The largest file under {0} is {1} with a length of {2} bytes",
                                      startFolder, longestFile.FullName, longestFile.Length);
      
                  //查找字節(jié)數(shù)最小的文件
                  var smallestFile = fileList.OrderBy(x => GetFileLength(x)).First();            
      
                  Console.WriteLine("The smallest file under {0} is {1} with a length of {2} bytes",
                                      startFolder, smallestFile.FullName, smallestFile.Length);
      
                 //查找字節(jié)前十大的文件
                  var queryTenLargest = fileList.OrderByDescending(x => GetFileLength(x)).Take(10);        
      
                  Console.WriteLine("The 10 largest files under {0} are:", startFolder);
      
                  foreach (var v in queryTenLargest)
                  {
                      Console.WriteLine("{0}: {1} bytes", v.FullName, v.Length);
                  }
              }
      
              static long GetFileLength(System.IO.FileInfo fi)
              {
                  long bytes;
                  try
                  {
                      bytes = fi.Length;
                  }
                  catch (System.IO.FileNotFoundException)
                  {
                      //如果文件找不到 0字節(jié)處理
                      bytes = 0;
                  }
                  return bytes;
              }
          }   
      }
      View Code

      查詢目錄樹中重復文件。

      using System;
      using System.Collections.Generic;
      using System.Data;
      using System.IO;
      using System.Linq;
      
      namespace ConsoleApp4
      {
          class Program
          {
              static void Main(string[] args)
              {
                  string startFolder = @"C:\Users\biubiu\Desktop\代碼\異步\ConsoleApp4\Test\";
                  DirectoryInfo dir = new DirectoryInfo(startFolder);           
                  IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
      
                  //根據(jù)文件名字、最后更新時間、字節(jié)長度來判斷文件重復
                  var querySameGroup = from file in fileList
                                           group file.Name by new PortableKey { Name = file.Name, LastWriteTime = file.LastWriteTime, Length = file.Length } into fileGroup
                                           where fileGroup.Count() > 1
                                           select fileGroup;
      
                  //Linq 方法調用寫法 
                  //var querySameGroup2 = fileList.GroupBy(file => new PortableKey
                  //{
                  //    LastWriteTime = file.LastWriteTime,
                  //    Length = file.Length,
                  //    Name = file.Name
                  //}, file => file.Name).Where(fileGroup => fileGroup.Count() > 1);
      
                  foreach(var group in querySameGroup)
                  {
                      Console.WriteLine(group.Key);
                      foreach(var item in group)
                      {
                          Console.WriteLine($"  {item}");
                      }
                  }        
              }       
          }
      
          class PortableKey
          {
              public string Name { get; set; }
              public DateTime LastWriteTime { get; set; }
              public long Length { get; set; }
      
              //分組跟這個相關
              public override bool Equals(object obj)
              {
                  PortableKey other = (PortableKey)obj;
                  return this.LastWriteTime == other.LastWriteTime &&
                      this.Length == other.Length &&
                      this.Name == other.Name;
              }
      
              public override int GetHashCode()
              {
                  string str = $"{this.LastWriteTime}{this.Length}{this.Name}";
                  return str.GetHashCode();
              }
              public override string ToString()
              {
                  return $"{this.Name} {this.Length} {this.LastWriteTime}";
              }
          }
      }

       

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多