Tape drive photo by P. Hollenback |
Unfortunately there's no built-in way to check for the available free storage size / free disk space at those locations, although there are scenarios where that information can be essential, especially when dealing with removable devices. But no worries, the good old Win32 has it covered with the GetFreeDiskSpaceEx function and it can be used with Windows 8 and Windows Phone 8. In order to use it from managed code it just has to be called through P/Invoke or wrapped in a WinRT component which I prefer nowadays, therefore this post provides a short WinRT C++/Cx snippet.
I assume you know how to create a custom C++/Cx WinRT component with Visual Studio, if not go back to my previous blog post which shows just that.
How it works
- Open the generated precompiled header file pch.h of your WinRT component's Visual Studio project and add an include for windows.h:
// pch.h - Header for standard system include files. #pragma once #include <windows.h>
- Open the header file of your component and add the method declaration of GetAvailableBytes to your WinRT component which will internally use the GetFreeDiskSpaceEx, but only takes a path as string parameter and returns a WinRT uint64 (unsigned long) type with the available free space in bytes.
namespace MyNativeStuff { public ref class MyStorageExtensions sealed { public: uint64 GetAvailableBytes(Platform::String^ path); }; }
- Add the method implementation in the source file of the component (.cpp).
The GetDiskFreeSpaceEx function takes a pointer to an ULARGE_INTEGER which is an union from ancient times when compilers didn't support 64 bit types. Our method then returns the filled unsigned long QuadPart of it.
#include "pch.h" #include "MyNativeStuff.h" using namespace MyNativeStuff; using namespace Platform; uint64 MyStorageExtensions::GetAvailableBytes(Platform::String^ path) { ULARGE_INTEGER availableBytesToCaller; availableBytesToCaller.QuadPart = 0; GetDiskFreeSpaceEx(path->Data(), &availableBytesToCaller, NULL, NULL); return availableBytesToCaller.QuadPart; }
- You are now ready to use the component in your managed C# code.
var myNativeComponent = new MyNativeStuff.MyStorageExtensions(); // Create a folder in temp folder of the app and check the available free space var myTempFolder = await ApplicationData.Current.TemporaryFolder.CreateFolderAsync("test", CreationCollisionOption.OpenIfExists); var availableSpaceTempFolder = myNativeComponent.GetAvailableBytes(myTempFolder.Path); // Create a folder in videos lib and check the available free space var myVideoFolder = await KnownFolders.VideosLibrary.CreateFolderAsync("test", CreationCollisionOption.OpenIfExists); var availableSpaceVideoLib = myNativeComponent.GetAvailableBytes(myVideoFolder.Path);
Note, the GetFreeDiskSpaceEx works with any directory path the calling code is allowed to access.