简体   繁体   English

C#如何使用Directory.GetFiles()获取与Windows资源管理器中的顺序相同的文件?

[英]C# How do I use Directory.GetFiles() to get files that have the same order as in Windows explorer?

Is is possible to get files that is ordered same as in Windows Explorer 可以获取与Windows资源管理器中顺序相同的文件

I know "natural sort", but it's not what I need, I need to get the file list ordered by the same attribute in Windows Explorer, for example: 我知道“自然排序”,但这不是我所需要的,我需要按Windows资源管理器中相同属性排序的文件列表,例如:

If I ordered a directory by the attribute "create date", then I will get a file list as below: 如果按属性“创建日期”订购目录,则将获得如下文件列表:

name                    create date    file size
1.txt                   2012/1/1        125Kb
2.tab                   2012/3/2        15Kb
3.bmp                   2013/5/5        26Kb

If my windows explorer order file list with the attribute "file size", the the file list would be: 如果我的Windows资源管理器订购的文件列表的属性为“文件大小”,则文件列表为:

name                     create date    file size
2.tab                    2012/3/2        15Kb
3.bmp                    2013/5/5        26Kb
1.txt                    2012/1/1        125Kb

Could anyone help? 有人可以帮忙吗?

Here's how to get a list of files sorted by their name: 以下是获取按文件名排序的文件列表的方法:

var path = @"C:\windows"; // obviously change this to whatever you want
var files = System.IO.Directory.GetFiles (path).ToList ();
file.Sort();

And that's it! 就是这样!

Here's how you would do it per your given code sample: 这是按照给定代码示例执行的操作:

var temperaturePressureSignalFilesList = Directory.GetFiles(TemperaturePressureSignalDirectory, "*.txt", SearchOption.TopDirectoryOnly).ToList();
temperaturePressureSignalFilesList.Sort();

using System.Linq; 使用System.Linq;

DirectoryInfo info = new DirectoryInfo(""); DirectoryInfo info = new DirectoryInfo(“”); FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray(); FileInfo [] files = info.GetFiles()。OrderBy(p => p.CreationTime).ToArray(); foreach (FileInfo file in files) { // DO Something... } foreach(文件中的FileInfo文件){//做某事...}

here is the sample code for get files in directory by creation time. 这是按创建时间获取目录中文件的示例代码。

You can get files by size same way. 您可以通过相同的方式获取文件大小。

I think this is going to be a lot more complex than you expect. 我认为这将比您预期的要复杂得多。 Folder settings are stored in the registry in two places: 文件夹设置存储在注册表中的两个位置:

HKCU\Software\Microsoft\Windows\Shell\BagMRU
HKCU\Software\Microsoft\Windows\Shell\Bags

The first path contains a structure which reflects the structure of the file system, and the second path contains details about those items, including a REG_BINARY value called "Sort" which records the sort order used for that folder. 第一个路径包含反映文件系统结构的结构,第二个路径包含有关那些项目的详细信息,包括一个称为“ Sort”的REG_BINARY值,该值记录了用于该文件夹的排序顺序。

See Willi Balenthin's website for details on the structure, including sample code (in Python) 有关结构的详细信息,请参见Willi Balenthin的网站 ,包括示例代码(使用Python)

If you want natural sort order, you should either P/Invoke StrCmpLogicalW ( http://msdn.microsoft.com/en-us/library/bb759947.aspx ) or find a managed natural sort algorithm. 如果要自然排序顺序,则应该P / Invoke StrCmpLogicalWhttp://msdn.microsoft.com/en-us/library/bb759947.aspx )或查找托管自然排序算法。 There is no built-in natural sort in .NET Framework. .NET Framework中没有内置的自然排序。

I think you cannot know which is the order in the pane (by size, name or whatever), you must read the list and then sort it the way you want or prompt the user to select a sorting attribute. 我认为您不知道窗格中的顺序是什么(按大小,名称或其他方式),您必须阅读列表,然后以所需的方式对其进行排序,或者提示用户选择排序属性。

As Kenny posted Sorting Directory.GetFiles() here is an approach, anyway I still thinking there is no possibly way to know which is the sorting order that user selected in the viewing pane. 正如肯尼 Kenny)发布的Sorting Directory.GetFiles()此处介绍的一种方法,无论如何,我仍然认为没有办法知道用户在查看窗格中选择的排序顺序。

I guess you are talking about viewing pane in Windows Explorer (it's essentially a Windows File Manager but also known under different name). 我猜您在谈论Windows资源管理器中的查看窗格(它本质上是Windows文件管理器,但也以不同的名称而闻名)。 Some clarification is needed. 需要澄清。 You can apply your custom sorting on various columns; 您可以在各种列上应用自定义排序。 moreover, you can have multiple viewing panes (windows) open sorted on different columns. 此外,您可以在不同的列上打开多个查看窗格(窗口)。 Thus, the problem definition is a bit unclear. 因此,问题的定义还不清楚。

Assuming that you know the sorting order in your viewing panes, then you can use System.IO.DirectoryInfo and derived FileSystemInfo[] objects; 假设您知道查看窗格中的排序顺序,则可以使用System.IO.DirectoryInfo和派生的FileSystemInfo[]对象; the latter has files.OrderBy method. 后者具有files.OrderBy方法。 Hope this will help. 希望这会有所帮助。 My best, Alex 我最好的Alex

I think you would have to write a shell extension for windows explorer that captures sort events on columns and writes that metadata to disk in some structured way. 我认为您必须为Windows资源管理器编写一个shell扩展,以捕获列上的排序事件并将该元数据以某种结构化方式写入磁盘。 You may have multiple explorer windows open so might be an idea to apply timestamp or id so you know which explorer window you are dealing with. 您可能打开了多个资源管理器窗口,因此可能是应用时间戳或ID的一个主意,以便您知道要处理哪个资源管理器窗口。 Then in your app read that metadata to get the sort order and apply accordingly. 然后在您的应用中读取该元数据以获取排序顺序并相应地应用。 Not easy but doable. 不容易,但可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM