【问题标题】:Sorting directory files and getting the highest file name对目录文件进行排序并获得最高的文件名
【发布时间】:2012-07-27 15:54:27
【问题描述】:

我有一个目录,其中包含 40 个文件,名称从 0 到 39(例如), 我正在尝试获取名称中数字最大的文件(这意味着我需要获取“39”) 我正在尝试对目录进行排序.. 我尝试过使用以下主题:

How to retrieve list of files in directory, sorted by name

Sorting the result of Directory.GetFiles in C#

没有什么对我有用.. 我尝试了每种方法 - 使用 Linq 和其他方法.. 我不知道为什么..

我得到以下排序结果(查看下图):

感谢您的帮助,

丁布拉查。

【问题讨论】:

    标签: c# wpf file sorting


    【解决方案1】:

    这样排序是合乎逻辑的,你会引入一些语义来按数字排序,即将所有文件名解析为数字,然后按数字对文件进行排序。

    有点像

    files.OrderBy(path => Int32.Parse(Path.GetFileNameWithoutExtension(path)))
    

    使用Last()获取编号最大的文件。

    【讨论】:

    • TryParse() 处理任何不寻常的事情而不抛出异常。
    • @HABO:如果对每个文件进行错误处理有意义的话。
    • @H.B. TryParse 会更快,因为您可以避免使用 try-catch 块,如果存在无效文件,则速度会更快。
    【解决方案2】:

    Windows StrCmpLogicalW 中有一个本机函数,它将在字符串中将数字作为数字而不是字母进行比较。制作一个调用该函数并将其用于比较的比较器很容易。

    public class StrCmpLogicalComparer : Comparer<string>
    {
        [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]
        private static extern int StrCmpLogicalW(string x, string y);
    
        public override int Compare(string x, string y)
        {
            return StrCmpLogicalW(x, y);
        }
    }
    

    这是一个示例程序,它将显示默认排序和StrCmpLogicalW 排序之间的差异

    class Program
    {
        static void Main()
        {
            List<string> items = new List<string>()
            {
                "Example1.txt", "Example2.txt", "Example3.txt", "Example4.txt", "Example5.txt", "Example6.txt", "Example7.txt", "Example8.txt", "Example9.txt", "Example10.txt",
                "Example11.txt", "Example12.txt", "Example13.txt", "Example14.txt", "Example15.txt", "Example16.txt", "Example17.txt", "Example18.txt", "Example19.txt", "Example20.txt"
            };
    
            items.Sort();
    
            foreach (var item in items)
            {
                Console.WriteLine(item);
            }
    
            Console.WriteLine();
    
            items.Sort(new StrCmpLogicalComparer());
    
            foreach (var item in items)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
    

    哪个输出

    Example1.txt
    Example10.txt
    Example11.txt
    Example12.txt
    Example13.txt
    Example14.txt
    Example15.txt
    Example16.txt
    Example17.txt
    Example18.txt
    Example19.txt
    Example2.txt
    Example20.txt
    Example3.txt
    Example4.txt
    Example5.txt
    Example6.txt
    Example7.txt
    Example8.txt
    Example9.txt
    
    Example1.txt
    Example2.txt
    Example3.txt
    Example4.txt
    Example5.txt
    Example6.txt
    Example7.txt
    Example8.txt
    Example9.txt
    Example10.txt
    Example11.txt
    Example12.txt
    Example13.txt
    Example14.txt
    Example15.txt
    Example16.txt
    Example17.txt
    Example18.txt
    Example19.txt
    Example20.txt
    

    【讨论】:

      【解决方案3】:

      这是 VB.NET 检索最高编号的名称。将 OrderByDescending 键更改为 x.LastWriteTime 可获得最新文件。

          Dim OldName As String = String.Empty
          Dim DI As New IO.DirectoryInfo("C:\")
          For Each FI As IO.FileInfo In DI.GetFiles("*.*").OrderByDescending(Function(x) x.Name)
              OldName = FI.FullName
              Exit For
          Next
      

      【讨论】:

        猜你喜欢
        • 2020-07-30
        • 2011-07-04
        • 2016-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        相关资源
        最近更新 更多