Search code examples
iotwindows-10-iot-corewindowsiot

Detect available storagefolder space in Windows 10 IoT?


Is there a way to detect the amount of available space in a storagefolder? I am using the code below to write a text file to the directory but would like to know how much space is available. My app will be writing audio files to the directory and would like to be able detect the available storage space. Thanks

var storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
var storageFile = await storageFolder.CreateFileAsync("Test.txt", Windows.Storage.CreationCollisionOption.GenerateUniqueName);

Solution

  • You can use StorageFolder.Properties.RetrievePropertiesAsync() api.

    I tested with the following piece of code:

            //Get the available space
            var storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
            var result = await storageFolder.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace" });
            var freeSpace = result["System.FreeSpace"];
    
            //Do something to take up some space of "Data" folder
            byte[] data = new byte[1024000];
            var storageFile = await storageFolder.CreateFileAsync("Test.txt", Windows.Storage.CreationCollisionOption.GenerateUniqueName);
            await FileIO.WriteBytesAsync(storageFile,data);
    
            //Get the available space
            var result2 = await storageFolder.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace" });
            var freeSpace2 = result2["System.FreeSpace"];