zoukankan      html  css  js  c++  java
  • DirectInfo.GetFiles返回数组的默认排序

    NTFS和CDFS下,是按照字母顺序,而FAT下,按照文件创建时间顺序

      1 using System;
      2 using System.Collections;
      3 using System.IO;
      4 
      5 namespace ConsoleApplication1
      6 {
      7     /// <summary>
      8     /// 文件排序类
      9     /// </summary>
     10     public class FileSort : IComparer
     11     {
     12         private FileOrder _fileorder;
     13         private FileAsc _fileasc;
     14 
     15         /// <summary>
     16         /// 构造函数
     17         /// </summary>
     18         public FileSort()
     19             : this(FileOrder.Name, FileAsc.Asc)
     20         { }
     21 
     22         /// <summary>
     23         /// 构造函数
     24         /// </summary>
     25         /// <param name="fileorder"></param>
     26         public FileSort(FileOrder fileorder)
     27             : this(fileorder, FileAsc.Asc)
     28         { }
     29 
     30         /// <summary>
     31         /// 构造函数
     32         /// </summary>
     33         /// <param name="fileorder"></param>
     34         /// <param name="fileasc"></param>
     35         public FileSort(FileOrder fileorder, FileAsc fileasc)
     36         {
     37             _fileorder = fileorder;
     38             _fileasc = fileasc;
     39         }
     40 
     41         /// <summary>
     42         /// 比较函数
     43         /// </summary>
     44         /// <param name="x"></param>
     45         /// <param name="y"></param>
     46         /// <returns></returns>
     47         public int Compare(object x, object y)
     48         {
     49             FileInfo file1 = x as FileInfo;
     50             FileInfo file2 = y as FileInfo;
     51             FileInfo file3;
     52 
     53             if (file1 == null || file2 == null)
     54                 throw new ArgumentException("参数不是FileInfo类实例.");
     55 
     56             if (_fileasc == FileAsc.Desc)
     57             {
     58                 file3 = file1;
     59                 file1 = file2;
     60                 file2 = file3;
     61             }
     62 
     63             switch (_fileorder)
     64             {
     65                 case FileOrder.Name:
     66                     return file1.Name.CompareTo(file2.Name);
     67                 case FileOrder.Length:
     68                     return file1.Length.CompareTo(file2.Length);
     69                 case FileOrder.Extension:
     70                     return file1.Extension.CompareTo(file2.Extension);
     71                 case FileOrder.CreationTime:
     72                     return file1.CreationTime.CompareTo(file2.CreationTime);
     73                 case FileOrder.LastAccessTime:
     74                     return file1.LastAccessTime.CompareTo(file2.LastAccessTime);
     75                 case FileOrder.LastWriteTime:
     76                     return file1.LastWriteTime.CompareTo(file2.LastWriteTime);
     77                 default:
     78                     return 0;
     79             }
     80         }
     81     }
     82 
     83     /// <summary>
     84     /// 排序依据
     85     /// </summary>
     86     public enum FileOrder
     87     {
     88         /// <summary>
     89         /// 文件名
     90         /// </summary>
     91         Name,
     92         /// <summary>
     93         /// 大小
     94         /// </summary>
     95         Length,
     96         /// <summary>
     97         /// 类型
     98         /// </summary>
     99         Extension,
    100         /// <summary>
    101         /// 创建时间
    102         /// </summary>
    103         CreationTime,
    104         /// <summary>
    105         /// 访问时间
    106         /// </summary>
    107         LastAccessTime,
    108         /// <summary>
    109         /// 修改时间
    110         /// </summary>
    111         LastWriteTime
    112     }
    113 
    114     /// <summary>
    115     /// 升序降序
    116     /// </summary>
    117     public enum FileAsc
    118     {
    119         /// <summary>
    120         /// 升序
    121         /// </summary>
    122         Asc,
    123         /// <summary>
    124         /// 降序
    125         /// </summary>
    126         Desc
    127     }
    128 }
     1 using System;
     2 using System.IO;
     3 
     4 namespace ConsoleApplication1
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             DirectoryInfo dirInfo = new DirectoryInfo(@"F:abc");
    11             FileInfo[] lstFile = dirInfo.GetFiles();
    12             Array.Sort(lstFile, new FileSort(FileOrder.LastWriteTime)); //按修改日期升序排列
    13             foreach (FileInfo file in lstFile)
    14                 Console.WriteLine(file.Name);
    15 
    16             Console.Read();
    17 
    18         }
    19     }    
    20 }
  • 相关阅读:
    性能测试基本功 手动配置nginx+phpcgi+zend+mysql
    MySQL的show full processlist命令
    LoadRunner测试Webservices的例子
    Firefox 5 公开测试下载
    Shunra 即将发布 PerformanceSuite 7.0 和NetworkCatcher 7.0
    LoadRunner常见问题
    动态生成的图片保存成png格式
    如何解决ASP.NET程序安装到繁体系统上的乱码问题
    在Ubuntu上下载、编译和安装Android最新源代码
    腾讯微博api的蛋疼问题
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/3259640.html
Copyright © 2011-2022 走看看