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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Shawn Lee
  • 103
  • 2
  • 8
  • The windows explorer sort order can be anything. What do you want it sorted by? – Esteban Araya Aug 03 '12 at 04:47
  • can explain why you need this kind of functionality? give some more brief detail about that – Krunal Mevada Aug 03 '12 at 04:52
  • 1
    possible duplicate of [How would I sort a list of files by name to match how Windows Explorer displays them?](http://stackoverflow.com/questions/1012985/how-would-i-sort-a-list-of-files-by-name-to-match-how-windows-explorer-displays) – Jeremy Thompson Aug 03 '12 at 05:21
  • Can you post the link of the misunderstood solution ? maybe we can translate ? – sstassin May 07 '13 at 04:50
  • It was not misunderstood, infact it was difficult to understand for a new bie like me. – Asad Waheed May 07 '13 at 04:53
  • @AsadWaheed, sstassin is asking for you to post your code that attempts to sort the Directory.GetFiles() list. This might help you get started http://stackoverflow.com/questions/52842/sorting-directory-getfiles – kenny May 07 '13 at 04:55
  • Here is the code I am using for sorting the file retrieved from a directory
     string[] temperaturePressureSignalFilesList = Directory.GetFiles(TemperaturePressureSignalDirectory, "*.txt", SearchOption.TopDirectoryOnly);
    – Asad Waheed May 07 '13 at 04:58
  • Check this http://stackoverflow.com/questions/1012985/how-would-i-sort-a-list-of-files-by-name-to-match-how-windows-explorer-displays – Guru Kara May 07 '13 at 05:16

7 Answers7

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)

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
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();
PhilChuang
  • 2,556
  • 1
  • 23
  • 29
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.

seokrae.kim
  • 304
  • 3
  • 9
  • 1
    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~~ – Shawn Lee Aug 03 '12 at 04:25
  • @ShawnLee i think you have to get windows explorer's order settings before get files. retrieving information key from registry something. – seokrae.kim Aug 03 '12 at 04:30
  • 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 http://stackoverflow.com/a/21084920/1429390 with some error checking seems safer to me. What do you think? – Stéphane Gourichon Jan 07 '16 at 18:29
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

Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
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.

Andrey Shchekin
  • 21,101
  • 19
  • 94
  • 162
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.

Community
  • 1
  • 1
io_exception
  • 154
  • 1
  • 13
  • 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 80 – Asad Waheed May 07 '13 at 04:54
  • @AsadWaheed Right; the pane is doing its own sorting, and you're going to have to as well. – dlev May 07 '13 at 04:56
  • I need my sort, how could i achieve this sort? – Asad Waheed May 07 '13 at 05:00
  • @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. – dlev May 07 '13 at 05:02
  • You see there is a unanswered question in SO about getting the sorted field from windows explorer http://stackoverflow.com/questions/14401016/get-the-field-on-which-the-explorer-window-is-sorted – Guru Kara May 07 '13 at 05:15
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.

ComeIn
  • 1,519
  • 17
  • 12