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

7

6

Is is possible to get files that is ordered same as in Windows Explorer

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:

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:

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?

Symphysis answered 3/8, 2012 at 3:44 Comment(8)
The windows explorer sort order can be anything. What do you want it sorted by?Prognosis
can explain why you need this kind of functionality? give some more brief detail about thatChaunceychaunt
possible duplicate of How would I sort a list of files by name to match how Windows Explorer displays them?Skidway
Can you post the link of the misunderstood solution ? maybe we can translate ?Diadromous
It was not misunderstood, infact it was difficult to understand for a new bie like me.Supercharge
@AsadWaheed, sstassin is asking for you to post your code that attempts to sort the Directory.GetFiles() list. This might help you get started https://mcmap.net/q/109772/-sorting-directory-getfilesRevivalism
Here is the code I am using for sorting the file retrieved from a directory <pre> string[] temperaturePressureSignalFilesList = Directory.GetFiles(TemperaturePressureSignalDirectory, "*.txt", SearchOption.TopDirectoryOnly);<code>Supercharge
Check this #1013485Lalalalage
C
4

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.

See Willi Balenthin's website for details on the structure, including sample code (in Python)

Cru answered 3/8, 2012 at 4:41 Comment(1)
Thank you sir, that's what I want.Symphysis
P
3

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();
Pinky answered 7/5, 2013 at 5:3 Comment(0)
C
2

using System.Linq;

DirectoryInfo info = new DirectoryInfo(""); FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray(); foreach (FileInfo file in files) { // DO Something... }

here is the sample code for get files in directory by creation time.

You can get files by size same way.

Caresse answered 3/8, 2012 at 3:56 Comment(3)
hmm...but I don't want use OrderBy() function, I need to get files orderd by the sorting attributes in Windows Explorer,for example, if files in Windows Explorer are ordered by CreateionTime then I can get the file list ordered by CreattionTime, if files are ordered by "File Size" in Windows Explorer, I can get file list ordered by "File Size"....It means my getfiles() function would return different result by different sorting attribute in file explorer, help please~~Symphysis
@ShawnLee i think you have to get windows explorer's order settings before get files. retrieving information key from registry something.Caresse
Won't Linq operators choke if some directory in a tree is renamed while Windows walks it? While less elegant, something in the line of https://mcmap.net/q/1772261/-directory-getdirectories-sort-by-name-c-duplicate with some error checking seems safer to me. What do you think?Hypethral
E
1

I guess you are talking about viewing pane in Windows Explorer (it's essentially a Windows File Manager but also known under different name). 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; the latter has files.OrderBy method. Hope this will help. My best, Alex

Enuresis answered 7/5, 2013 at 5:5 Comment(0)
P
1

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. There is no built-in natural sort in .NET Framework.

Pliner answered 7/5, 2013 at 5:11 Comment(0)
A
0

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.

Alienation answered 7/5, 2013 at 4:52 Comment(5)
ok for example: Here is the windows natural view pane for list of files, I am trying to sort 25 50 80 115 145 I want to retreive files in the same order as shown in above example. While GetFile() is giving me the list in the following order 115 145 25 50 80Supercharge
@AsadWaheed Right; the pane is doing its own sorting, and you're going to have to as well.Berbera
I need my sort, how could i achieve this sort?Supercharge
@AsadWaheed The Shell pane is sorting in a "natural" order, putting 25 ahead of 115, even though "115" comes before "25" in a lexical ordering. Getting a natural ordering right is tough, though doing a search for that term (and C#) should get you some straightforward examples.Berbera
You see there is a unanswered question in SO about getting the sorted field from windows explorer #14401516Lalalalage
I
0

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. 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. Then in your app read that metadata to get the sort order and apply accordingly. Not easy but doable.

Impoverish answered 23/9, 2014 at 0:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.