Search Unity

Get available disk space

Discussion in 'Scripting' started by Bypp, Jun 11, 2014.

  1. Bypp

    Bypp

    Joined:
    Jun 4, 2014
    Posts:
    29
    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):
    1. 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):
    1. 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):
    1. foreach(string drive inDirectory.GetLogicalDrives());.
    ... But that doesn't get me anywhere in terms of free space.
     
  2. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
  3. Bypp

    Bypp

    Joined:
    Jun 4, 2014
    Posts:
    29
    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 ?
     
  4. Bypp

    Bypp

    Joined:
    Jun 4, 2014
    Posts:
    29
  5. Bypp

    Bypp

    Joined:
    Jun 4, 2014
    Posts:
    29
    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 !
     
  6. KristofVerbiest

    KristofVerbiest

    Joined:
    Apr 29, 2014
    Posts:
    2
    Just for future reference, this is how you could do this in Android:

    Code (csharp):
    1. var jc =newAndroidJavaClass("android.os.Environment");
    2. var file = jc.CallStatic<AndroidJavaObject>("getDataDirectory");
    3. var path = file.Call<string>("getAbsolutePath");
    4.  
    5. var stat =newAndroidJavaObject("android.os.StatFs", path);
    6. var blocks = stat.Call<long>("getAvailableBlocksLong");
    7. var blockSize = stat.Call<long>("getBlockSizeLong");
    8.  
    9. var freeSize = blocks * blockSize;
     
    halbich likes this.
  7. dkrprasetya

    dkrprasetya

    Joined:
    Mar 3, 2016
    Posts:
    1
    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!
     
    Yleisnero, karsnen and vexe like this.
  8. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    638
    @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.
     
    Last edited: Mar 17, 2017
    zinchencko likes this.
  9. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    86
    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.
     
  10. Guillogika

    Guillogika

    Joined:
    Sep 18, 2017
    Posts:
    2
    Karsnen_2 likes this.
  11. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    86