如何按照像Windows资源管理器那样的顺序获取按上次修改的文件日期排序的目录中的顶级目录? [英] How do I get the top-directories within a directory sorted by last modified file date exact in the order like windows explorer does?

查看:98
本文介绍了如何按照像Windows资源管理器那样的顺序获取按上次修改的文件日期排序的目录中的顶级目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在最后修改的文件日期降序的排序顺序中仅获取给定目录中的顶级目录(不是任何递归)。这里的任务是,订单必须与Windows资源管理器完全一样,即使某些目录具有相同的文件日期。



此代码我试过Path1可以是任何工作文件夹:



I would like to get only the top-directories (not any recursive) within a given directory in the sort-order of the last modified file date descending. The quest here is, that the order must be exactly like windows explorer does, even if some directories have the same file date.

This code I have tried where Path1 can be any work folder:

Directory.GetDirectories(Path1, "*", SearchOption.TopDirectoryOnly).OrderByDescending(d => new DirectoryInfo(d).LastWriteTime).ToArray();





它喜欢工作而不起作用。有时它会检索预期的排序顺序,有时则不会。如果许多目录具有相同的文件日期,则订单会失真,这意味着它不再与Windows资源管理器排序顺序完全相等。有些文件被向上或向下移动或向中移动。

如果我使用旧的API-VB6代码执行相同的操作,我每次都会得到确切的排序顺序,即使文件日期是否相同 - 浏览器文件顺序保存在我检索的任何目录中的VB6应用程序中。

C#似乎做了不同的事情,或者上面的单行需要调整为更准确。我不知道这个,我不想用于这么简单的任务。



我尝试过的事情:



我也尝试按DirectoryInfo(d).LastWriteTimeUtc进行排序,但没有区别。我已检查过其他代码,但它们都使用LINQ,并且与我的代码有些相同。我不介意使用LINQ,但我需要像Windows资源管理器中那样的确切目录排序顺序(具有相同文件日期的任何内容都不应在列表中上移或下移!)。也许toArray()会扭曲顺序,但我需要它作为一个数组。到目前为止我找不到原因。



It like works and not works. Sometimes it retrieves the expected sort order, sometimes it doesn't. If many directories have identical file dates the order becomes distorted which means it doesn't equal any longer to the Windows Explorer sort order exactly. Some files are moved up or moved down or in the middle.
If I do the same with an old API-Code of VB6 I get the exact sort order every time, it doesn't matter even if the file dates are equal or not - the explorer file order is preserved in my VB6 application in any directory which I retrieve.
C# seems to do something different or the above one-liner needs to be adjusted to be more accurate. I have no idea on this, API I would not like to use for such trivial task.

What I have tried:

I tried also to sort by "DirectoryInfo(d).LastWriteTimeUtc" but it made no difference. I have checked for other codes, but they all use LINQ and are somewhat identical with my code. I don't mind to use LINQ, but I need the exact directory sort order like in Windows Explorer (nothing with the same file date shall be moved up or down in the listing!). Maybe the toArray() distorts the order, but I would need it as an array. So far I cannot find the reason.

推荐答案

你不能使用DirectoryInfo对象吗?



赞这个:



Can't you use DirectoryInfo object?

Like this:

{
	Comparison<FileSystemInfo> dirComp = (x, y) =>
	{
		int i = y.LastAccessTime.CompareTo(x.LastAccessTime);
		if (i == 0) {
			i = string.Compare(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase);
		}
		return i;
	};

OR

	DirectoryInfo info = new DirectoryInfo("Path1");

	var  topFolders = info.EnumerateDirectories().OrderByDescending(d => (d.LastAccessTime)).ThenBy(d => (d.Name)).Select(d => (d.FullName)).ToArray();
;
}


这篇关于如何按照像Windows资源管理器那样的顺序获取按上次修改的文件日期排序的目录中的顶级目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆