2

I wanted to create a simple IsEmpty(StorageFolder directory) method that works in WinRT apps, i.e. that uses the async API. I don't see a relevant method in the documentation for the StorageFolder class, and I also haven't found anything in my searches. I'm sorry if I've missed it!

I managed to create the following method, which works:

public static async Task<bool> IsEmpty(StorageFolder directory)
{
    var files = await directory.GetFilesAsync();
    if (files.Count > 0)
    {
        return false;
    }

    var folders = await directory.GetFoldersAsync();
    if (folders.Count > 0)
    {
        return false;
    }

    return true;
}

But... is there a cleaner way? Either native or that I could code... It should be a simple thing to check if a directory is empty, but I know I've faced problems before when simply trying to check if a directory or file exists using the async API of WinRT.

I'm also not entirely sure if the asynchronous calls to GetFilesAsync and GetFoldersAsync get every file/folder in the directory before returning, or if they can somehow only get a single item before realizing that Count will be higher than 0 (I'm thinking of lazy evaluation, like in the Haskell language, but this is C#...). If they could, I would be more at peace with this method :)

CanisLupus
  • 583
  • 5
  • 15
  • The [link](http://stackoverflow.com/questions/755574/how-to-quickly-check-if-folder-is-empty-net) regarded as a possible duplicate is related to the old API, which I can't use in WinRT. It must use the async methods provided by the new API – CanisLupus Nov 29 '14 at 20:21
  • Do you care if any nested folders exists or if there are nested empty directories it counts as empty? – Yuval Itzchakov Nov 29 '14 at 20:41
  • As in the method I created, calling IsEmpty over a directory should return true if no folders or files exist inside it. If a folder is inside, it is _not_ empty, even if said folder is itself empty. – CanisLupus Nov 29 '14 at 21:05
  • If what you want is a shallow copy, this should work fine. It isn't an overkill since you're returning a read only list which describes the file / folder, but doesn't open aby handle to it. – Yuval Itzchakov Nov 29 '14 at 21:31
  • Thanks for your help. I realize that, but it still just seems... too much. Even if the returned list is read-only, GetFilesAsync/GetFoldersAsync still has to create it in full, right? If a folder contains 1000 files, it will create a list of 1000 elements, only to then check if the count is greater than zero. The same for 1000 folders. – CanisLupus Nov 29 '14 at 21:39
  • Then again, I guess that if no native method exists, it could be impossible to do this in a better way. I'll see if somebody comes up with a better method. If not, I'll probably accept the fact that this is good enough. – CanisLupus Nov 29 '14 at 21:40
  • Ill try searching around see if i find anything. – Yuval Itzchakov Nov 29 '14 at 21:46

1 Answers1

3

StorageFolder.GetItemsAsync(0,1) will retrieve the first file or sub-folder:

public static async Task<bool> IsEmpty(StorageFolder directory)
{
    var items = await directory.GetItemsAsync(0,1);
    return items.Count == 0;
}

GetFilesAsync and GetFoldersAsync will return all of the files or folders. These calls don't know that you are only going to care about the count, and I doubt the compiler is smart enough to realize that and rewrite the calls to use filtered versions automatically.

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • Ha! That's more like it. I though so too, regarding the behavior of those methods. It was wishful thinking in my question :) – CanisLupus Nov 30 '14 at 10:56
  • If you only need to deal with non-brokered locations (your own private directories) it is fine. If you need to check brokered locations (eg Pictures library) it's not foolproof since the Storage APIs don't return hidden or system files (and you can't use Win32 APIs for those locations either). – Peter Torr - MSFT Dec 04 '14 at 07:47