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

前端 未结 7 988
萌比男神i
萌比男神i 2020-12-11 19:12

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 th

相关标签:
7条回答
  • 2020-12-11 19:38

    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.

    0 讨论(0)
  • 2020-12-11 19:59

    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)

    0 讨论(0)
  • 2020-12-11 19:59

    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();
    
    0 讨论(0)
  • 2020-12-11 20:01

    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

    0 讨论(0)
  • 2020-12-11 20:01

    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.

    0 讨论(0)
  • 2020-12-11 20:03

    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.

    0 讨论(0)
提交回复
热议问题