How to set view mode of File Explorer to Extra Large Icons and sort in Modified Date

kwok kan ho 21 Reputation points
2023-01-15T07:52:11.0066667+00:00

I open the File Explorer using the following c# code. I want to set the view mode of that File Explorer to "Extra Large Icons" and sort in Modified Date with Modified Date column displayed. Currently I use SendKeys to set the view mode but I cannot sort by Modified Date.


    //
    // Summary:
    //     Open Folder in file explorer
    //
    // Parameters:
    //   foldername:
    //     folder name
    //
    // Returns:
    //     Return true if folder exists
    public static bool ShowFolderInExplorer(string foldername)
    {
      SHELLEXECUTEINFO lpExecInfo = default(SHELLEXECUTEINFO);
      lpExecInfo.cbSize = Marshal.SizeOf<SHELLEXECUTEINFO>();
      lpExecInfo.lpVerb = "explore";
      lpExecInfo.nShow = 5;
      lpExecInfo.lpFile = foldername;
      return ShellExecuteEx(ref lpExecInfo);
    }
    
    
    
 
    public static bool ShowFolderInExplorerWnd(string foldername, int left, int top, int width, int height, string[] keys)
    {
      bool result = false;
      if (ShowFolderInExplorer(foldername))
      {
        Thread.Sleep(DEFAULT_Delay);
        foreach (KeyValuePair<IntPtr, string> openWindow in OpenWindowGetter.GetOpenWindows())
        {
          IntPtr key = openWindow.Key;
          uint processId = 0u;
          User32DLL.GetWindowThreadProcessId(key, out processId);
          Process processById = Process.GetProcessById((int)processId);
          if (processById.ProcessName.Equals("explorer", StringComparison.OrdinalIgnoreCase) && openWindow.Value.Equals(foldername, StringComparison.OrdinalIgnoreCase) && processById.MainWindowHandle.ToString() != "0")
          {
            if (User32DLL.IsIconic(key))
            {
              User32DLL.ShowWindow(key, 9);
            }

            User32DLL.SetWindowPos(key, IntPtr.Zero, left, top, width, height, User32DLL.SetWindowPosFlags.SWP_SHOWWINDOW);
            User32DLL.SetForegroundWindow(key);
            for (int i = 0; i < keys.Length; i++)
            {
              SendKeys.SendWait(keys[i]);
            }

            break;
          }
        }

        result = true;
      }

      return result;
    }   
    
    
 
    public static IDictionary<IntPtr, string> GetOpenWindows()
    {
      IntPtr shellWindow = GetShellWindow();
      Dictionary<IntPtr, string> windows = new Dictionary<IntPtr, string>();
      EnumWindows(delegate (IntPtr hWnd, int lParam)
      {
        if (hWnd == shellWindow)
        {
          return true;
        }

        if (!IsWindowVisible(hWnd))
        {
          return true;
        }

        int windowTextLength = GetWindowTextLength(hWnd);
        if (windowTextLength == 0)
        {
          return true;
        }

        StringBuilder stringBuilder = new StringBuilder(windowTextLength);
        GetWindowText(hWnd, stringBuilder, windowTextLength + 1);
        windows[hWnd] = stringBuilder.ToString();
        return true;
      }, 0);
      return windows;
    }
   
    
    
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Lynne Dong 0 Reputation points
    2023-01-16T00:38:49.3833333+00:00

    To set the view mode of the File Explorer to "Extra Large Icons" and sort in Modified Date with Modified Date column displayed, you can use the System.IO.File and System.IO.FileInfo classes. Here is an example of how you can modify the existing C# code to achieve that:

    1. First, after calling the ShowFolderInExplorer method, you can use the System.IO.Directory.GetFiles method to get an array of all files in the specified folder.
    2. Next, you can use the System.IO.FileInfo class to get the file modified date for each file in the array and add it to a list of custom objects that contains the file name, file path and modified date.
    3. After that, you can sort the list by the modified date using the List.Sort method and the IComparer interface.
    4. Finally, you can use the .NET framework's File and FileInfo classes to change the view mode of the File Explorer window to "Extra Large Icons" and sort the files by modified date.

    Here is an example of how you can achieve that:

        public static void ShowFolderInExplorerWithViewAndSort(string foldername)
        {
            if (ShowFolderInExplorer(foldername))
            {
                Thread.Sleep(DEFAULT_Delay);
                foreach (KeyValuePair<IntPtr, string> openWindow in OpenWindowGetter.GetOpenWindows())
                {
                    IntPtr key = openWindow.Key;
                    uint processId = 0u;
                    User32DLL.GetWindowThreadProcessId(key, out processId);
                    Process processById = Process.GetProcessById((int)processId);
                    if (processById.ProcessName.Equals("explorer", StringComparison.OrdinalIgnoreCase) && openWindow.Value.Equals(foldername, StringComparison.OrdinalIgnoreCase) && processById.MainWindowHandle.ToString() != "0")
                    {
                        if (User32DLL.IsIconic(key))
                        {
                            User32DLL.ShowWindow(key, 9);
                        }
    
                        // Get all files in the folder
                        string[] files = Directory.GetFiles(foldername);
    
                        // Create a list of custom objects that contains the file name, file path and modified date
                        List<FileDetails> fileDetails = new List<FileDetails>();
                        foreach (string file in files)
                        {
                            FileInfo fileInfo = new FileInfo(file);
                            fileDetails.Add(new FileDetails { FileName = fileInfo.Name, FilePath = fileInfo.FullName, ModifiedDate = fileInfo.LastWriteTime });
                        }
    
                        // Sort the list by the modified date
                        fileDetails.Sort((x, y) => x.ModifiedDate.CompareTo(y.ModifiedDate));
    
                        // Change the view mode of the File Explorer window to "Extra Large Icons"
                        SendKeys.SendWait("%V");
                        SendKeys.SendWait("X");
                        SendKeys.SendWait("{TAB}");
                        SendKeys.SendWait("{RIGHT}");
                        SendKeys.SendWait("{RIGHT}");
                        SendKeys.SendWait("{RIGHT}");
                        SendKeys.SendWait("{RIGHT}");
                        SendKeys.SendWait("{RIGHT}");
                       
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.