I am working in Unity C#. I'd like to get the free available disk space on the device I'm using. I've tried using System.IO.DriveInfo but it doesn't work as I receive: Code (csharp): NotImplementedException:The requested feature is not implemented. ...when I try to get the drives. I think this is only a Windows problem, but I haven't found a solution doing my research. I'm also wondering if this class will work on Android, iOS and Mac as well. Code (csharp): DriveInfo[] allDrives =DriveInfo.GetDrives(); Why is the code above not working? And is there a better solution that would work on all platforms? I've seen a workaround that only gets me the name of the drives : . Code (csharp): foreach(string drive inDirectory.GetLogicalDrives());. ... But that doesn't get me anywhere in terms of free space.
You need to create a plugin for that on IOs. Apparently, even the detection is not accurate depending of the version you test it. Here are some links for IOs. http://stackoverflow.com/questions/...ble-free-disk-space-on-the-iphone-ipad-device http://stackoverflow.com/questions/22030077/calculate-free-disk-space-on-ipad-iphone-using-unity-c
I'll look into those and test further ! My priority right now is to get this working on PC and Mac, does anyone have an idea concerning those ?
Found a neat solution for PC : http://www.java2s.com/Tutorial/CSharp/0520__Windows/Getfreediskspace.htm However, I can't seem to find anything about Mac ! Thanks for the help !
Would I have to use a plugin for Mac ? Or create something in XCode ? Kind of in the dark here, any pointers would be appreciated !
Just for future reference, this is how you could do this in Android: Code (csharp): var jc =newAndroidJavaClass("android.os.Environment"); var file = jc.CallStatic<AndroidJavaObject>("getDataDirectory"); var path = file.Call<string>("getAbsolutePath"); var stat =newAndroidJavaObject("android.os.StatFs", path); var blocks = stat.Call<long>("getAvailableBlocksLong"); var blockSize = stat.Call<long>("getBlockSizeLong"); var freeSize = blocks * blockSize;
By the way if you are still looking for this, I have tried to implement this "get available disk space" feature into a plugin: Asset store link (it's free!): http://u3d.as/qF1 I believe the best way to get the disk size accurately is to get the value from Native methods. I implemented this feature for Windows, Mac, iOS, and Androd, made each native plugins then compiled it into this one project. I made it open source. So if you're interested to see the implementation, I also attached the GitHub repository link on the Asset Store description. Please feel free take a look . Also feel free to contribute if you have any suggestions or find any to improve. I hope this is helpful!
@dkrprasetya Thanks for sharing your work! But why do you return sizes in MB? Should really be in Bytes. Makes it inconvenient for me to test things. Sure 1MB is small relative to today's hard drives/size standards. But from an API usability standpoint, it should let me deal with the smallest possible addressible unit of space. If I wanted KB, MB etc I would do that myself, or you could provide helper functions to do it. Also, wasn't sure what was up with the string.Parse that you do in the IOS code path? Can't you just cast or use System.Convert? Seemed unnecessary.
The above asset store link is expired. Does anyone have a solution to this? Unity + iOS : Just need to know free disk space available at any point in time. Thanks.
The link worked fine for me, but just in case: https://assetstore.unity.com/packages/tools/simple-disk-utils-59382 (I have not tried it)