c#获得排序的图像列表

本文关键字:图像 列表 排序 | 更新日期: 2023-09-27 18:05:09

我试图在一个特定的目录中获得图像路径字符串的排序列表。目录包含43张以数字命名的图片:"1.png", "2.png",等等。

这是我的代码:

  var sorted = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.png").OrderBy(f => f);           
  foreach (string img in sorted) 
  {
   Console.WriteLine(img);
  } 

得到的结果

C:'Users'itapi'OneDrive' Studio 2013'Projects'Draw'Draw'bin'Debug'1.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'10.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'11.pngC:'Users'itapi'OneDrive' download_download_download_download_download_visual Studio 2013'Projects'Draw'Draw'bin'Debug'12.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'13.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'14.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'15.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'16.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'17.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'18.pngC:'Users'itapi'OneDrive' ο ο ο ο ο 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'19.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'2.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'20.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'21.pngC:'Users'itapi'OneDrive' download_download_download_download_download_visual Studio 2013'Projects'Draw'Draw'bin'Debug'22.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'23.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'24.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'25.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'26.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'27.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'28.pngC:'Users'itapi'OneDrive' ο ο ο ο ο 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'29.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'3.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'30.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'31.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'32.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'33.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'34.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'35.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'36.pngC:'Users'itapi'OneDrive' ό ο ο ο ο ο ο 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'37.pngC:'Users'itapi'OneDrive' windows_ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'38.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'39.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'4.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'40.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'41.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'42.pngC:'Users'itapi'OneDrive' ό ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν ν 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'43.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'5.pngC:'Users'itapi'OneDrive' downloaddownloaddownloaddownloaddownloaddownloaddownloadstudio 2013'Projects'Draw'Draw'bin'Debug'6.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'7.pngC:'Users'itapi'OneDrive' m_ () _ () _ () _ () 'Visual Studio 2013'Projects'Draw'Draw'bin'Debug'8.pngC:'Users'itapi'OneDrive' Studio 2013'Projects'Draw'Draw'bin'Debug'9.png

由于某种原因,我不能得到一个正确的按字母顺序排序的列表,我也尝试使用LINQ Sort()方法,没有成功。

寻找解决方案

谢谢。

c#获得排序的图像列表

原因是它是一个字符串,所以它按照字符串排序规则排序。你要做的是取其中的数字部分并按它排序:

Directory.GetFiles(Directory.GetCurrentDirectory(), "*.png")
         .OrderBy(f => int.Parse(f.Split('.')[0])); 

如果不使用Split,您可以使用Scott的建议使用GetFileNameWithoutExtension:

Directory.GetFiles(Directory.GetCurrentDirectory(), "*.png")
         .OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f))); 

请记住,这是一个天真的解决方案,它假设所有的文件名都是(number).png。如果不是,可以先检查是否为数字,然后解析