Search code examples
delphifiremonkey

How to make an explorer like file manager in Delphi FMX?


There is no any ShellListView/ShellTreeView component In Delphi FMX. So, How to make an explorer like file manager in Delphi FMX? Just simple as to count how many files/folders, certain type files.. and create text , read/write files. How to deal file system in Delphi FMX? I can not find System.IOUtils in Delphi FMX. I think, maybe I totally mis-understand FMX framework. Any hints?

Thanks!!!

Mitchell Hu


Solution

  • Firemonkey is a visual framework. People get so caught up on the way Embarcadero have marketed it that they often don't realize that much of what can be done with VCL can also be done with Firemonkey - it just requires a different approach and perspective.

    With VCL, you'd deal with controls that are native to Windows itself. The VCL simply exposes these through the Delphi (and C++) languages.

    With Firemonkey, the controls are created with vector shapes and so they're not native to a specific platform. Because of this, it's possible to create any sort of interface and run it on numerous platforms. At the same time as FMX was released, Embarcadero made their units crossplatform which means that most of the logic and data units included with Delphi can function on any platform which Firemonkey supports. Some functions are still platform specific but the majority is platform inclusive thanks to clever usage of IFDEFs.

    With your example, the System.IOUtils isn't specific to a framework and it does work with Firemonkey. You do need to add it to your uses before you can work with it, but the unit includes everything you'd need to create a file manager. System.IOUtils.TDirectory contains routines for creating, renaming, deleting, traversing and manipulating the properties of directories. Many of the old example routines you can find on Google still work (you may want to look for 'recursive directory' examples). System.IOUtils.TFile provides a similar set of routines, but for files instead of directories while System.IOUtils.TPath provides them for paths instead.

    System.IOUtils on XE3 Docwiki

    Expansion 14 April 2013

    Using the TTreeView with TTreeViewItem children would work to create the directory structure in the visual.

    Using System.IOUtils.TDirectory.GetDirectories('C:\') would return a dynamic string array (TStringDynArray). Something like this may work (Note: Tested - Confirmed that the code example below works);

    var
      DirArraySize, i : Integer;
      DirArray : TStringDynArray;
      TreeItem : TTreeViewItem;
    begin
      DirArray := System.IOUtils.TDirectory.GetDirectories('C:\');
      DirArraySize := Length(DirArray);
      for i := 0 to DirArraySize-1 do
      begin
        TreeItem := TTreeViewItem.Create(TreeView1);
        TreeItem.text := DirArray[i];
        TreeItem.Parent := TreeView1;
      end;
    end;
    

    I've now tested the code above and after a small correction (changing MyArray to DirArray on line 6 as it should have been) and can confirm that it outputs a list of folders/directories in C:\ into a TTreeView. Making it recursive wouldn't be too difficult and perhaps I'll expand on that in the near future.