locked
[UWP]Fast Way To Calculate Folder Sizes RRS feed

  • Question

  • Hi,

    I'm trying to write an app which gives the user a nice overview how the disk is filled. It all works fine but unfortunately the scan is _very_ slow when using StorageFolder and StorageFile. 

    Does anybody know a better way how to scan a disk in a fast way? I did some com32 tests which were really fast but unfortunately they only work inside the app sandbox data folder.

    Any help is highly appreciated

    Thanks

    Edit: There is a related question here: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/f7be991a-65c9-48cb-90a3-ebfe81306715/faster-way-to-get-folder-size?forum=csharpgeneral

    The solution is a WIN32_FIND_DATA data structure. But this won't work with a UWP app.

    2nd Edit: Since it seems that there is no real solution for a fast scan, does anybody know if a UWP app can access the Windows search indexer and get at least parts of the scan from this source?

    Thanks again

    • Edited by Active Geometry Saturday, April 9, 2016 5:14 AM Add tag
    • Edited by hish el Thursday, April 14, 2016 1:43 PM Added another approach
    Thursday, April 7, 2016 1:53 PM

Answers

  • Yes. But not fast enough.
    ...The same task with StorageFolder takes at least 20 minutes.

    Hmm, yes I think you're right there.

    In a very unscientific test comparing a UWP version of an existing
    Win32 application I have that enumerates files & folders, the UWP
    version is much slower than the Win32 version despite the UWP version
    doing considerably less.

    Maybe UWP isn't an appropriate vehicle for your scenario. :(

    Dave

    • Marked as answer by hish el Thursday, April 14, 2016 11:17 AM
    Friday, April 8, 2016 8:26 PM

All replies

  • Hello, I wrote this small recursive function which seems to work pretty well (on my 11Gb pics folder it did it in 32 millisecond):

        public static long FolderSize(string fullPath)
        {
          if (Directory.Exists(fullPath))
          {
            long result = 0;
            DirectoryInfo initialDirectory = new DirectoryInfo(fullPath);
            var files = initialDirectory.GetFiles();
            foreach (FileInfo file in files)
            {
              result += file.Length;
            }
            var directories = initialDirectory.GetDirectories();
            foreach (DirectoryInfo directory in directories)
            {
              result += FolderSize(directory.FullName);
            }
            return result;
          }
          else
            return -1;
        }

    Thursday, April 7, 2016 3:00 PM
  • Hi,

    thanks for your fast response.

    However, I think that DirectoryInfo which is in the namespace System.IO is not accessible by universal apps.

    Also, it would be great if you could scan a folder with a lot of items. 11GB of images are only a few thousand files max.

    Thanks again for your help

    Thursday, April 7, 2016 3:05 PM
  • From a first look, that seems to be more elaborate than I thought. Let me understand the problem clearly: so you want to access folders in your app's install directory only, or in any given global path?

    EDIT: I tried the earlier code on an 18Gb folder with basically the archives of my work and studies for the last 10 years (so quite a hierarchy) and it took 9 seconds. I'm sure we can do better but I'll focus on the Windows.Storage namespace solution.

    Also could you please include your current code?

    • Edited by Yazid HAMDI Friday, April 8, 2016 9:31 AM Info complement
    Friday, April 8, 2016 9:28 AM
  • From a first look, that seems to be more elaborate than I thought. Let me understand the problem clearly: so you want to access folders in your app's install directory only, or in any given global path?

    EDIT: I tried the earlier code on an 18Gb folder with basically the archives of my work and studies for the last 10 years (so quite a hierarchy) and it took 9 seconds. I'm sure we can do better but I'll focus on the Windows.Storage namespace solution.

    Also could you please include your current code?

    The above code is wrong, he can;'t call these things from the UWP platform.

    The only thing he can do is get all the StorageFiles in the current StorageFolder, then add up their sizes.

    Friday, April 8, 2016 1:36 PM
  • >The only thing he can do is get all the StorageFiles in the current StorageFolder, then add up their sizes.

    Not sure what you mean there. Given a root folder, an application can
    recursively enumerate its sub-folders.

    Dave

    Friday, April 8, 2016 3:06 PM
  • Yes. But not fast enough. I want to write a small app which shows the disk usage in a pretty way. Therefor I need to scan the whole disk (or a specific directory) as fast as possible. StorageFolder and StorageFile seem to be too bloated for this task. Using POSIX methods on OS-X or Linux takes about 1-2 minutes for a 500GB SSD with about one million files. The same task with StorageFolder takes at least 20 minutes.

    Using WIN32_FIND_DATA is also pretty fast but unfortunately it only works within the given sandboxed directories for the app. I don't know a way to get it to work on a user selected directory.

    Friday, April 8, 2016 7:04 PM
  • Yes. But not fast enough.
    ...The same task with StorageFolder takes at least 20 minutes.

    Hmm, yes I think you're right there.

    In a very unscientific test comparing a UWP version of an existing
    Win32 application I have that enumerates files & folders, the UWP
    version is much slower than the Win32 version despite the UWP version
    doing considerably less.

    Maybe UWP isn't an appropriate vehicle for your scenario. :(

    Dave

    • Marked as answer by hish el Thursday, April 14, 2016 11:17 AM
    Friday, April 8, 2016 8:26 PM
  • Hi hish el,

    As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags


    Best Regards,
    guang9Bear


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Saturday, April 9, 2016 5:14 AM
  • Maybe UWP isn't an appropriate vehicle for your scenario. :(

    I'm afraid that you're right. The question remains if there's a way to get another approach into the app store.

    Hish

    Saturday, April 9, 2016 2:58 PM
  • Ignoring the pointless win32 vs UWP conversation, there are ways of doing this but it requires some more fiddling.

    For starters, you can maintain an index of storage files and their sizes. This way, once you populated the index, you only need to query it and not the storage itself. You can do this with background tasks and ExtendedExecutionSession.

    The tricky part is keeping the index in sync effectively.

    • Proposed as answer by Active Geometry Thursday, April 14, 2016 2:23 AM
    • Marked as answer by Active Geometry Thursday, April 14, 2016 11:05 AM
    • Unmarked as answer by hish el Thursday, April 14, 2016 11:17 AM
    • Unproposed as answer by hish el Thursday, April 14, 2016 11:19 AM
    Monday, April 11, 2016 12:39 PM
  • Hi,

    thanks for your reply. I think what you are talking about is a background scan rather than a fast scan.

    Thursday, April 14, 2016 11:17 AM
  • Yup. Sort of like how groove/xbox music keeps the files in sync.

    You can keep the user away while the app scans with extended splash screens.

    Thursday, April 14, 2016 12:58 PM