Esrath Muqayyar
Esrath Muqayyar

Reputation: 105

How to get free internal storage space using Xamarin IOS

I can get RAM details using (NSProcessInfo.ProcessInfo.PhysicalMemory). But i want to get free internal device storage using Xamarin IOS.

Upvotes: 2

Views: 1222

Answers (2)

FreakyAli
FreakyAli

Reputation: 16449

I have converted your code into Xamarin.iOS and it is as follows:

private ulong GetFreeDiskspace()
    {
        ulong totalSpace = 0;
        ulong totalFreeSpace = 0;
        NSError error = null;
        string[] paths = NSSearchPath.GetDirectories(NSSearchPathDirectory.UserDirectory, NSSearchPathDomain.All);
        var defManager = NSFileManager.DefaultManager;
        var dicAttributes = defManager.
            GetFileSystemAttributes(paths.First()
            , out error);
        totalSpace = dicAttributes.Size;
        totalFreeSpace = dicAttributes.FreeSize;
        return totalFreeSpace;
    } 

Good luck!

In case of queries revert!

Upvotes: 0

Bruno Caceiro
Bruno Caceiro

Reputation: 7189

The method for getting the internal free space is this one:

  NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;

If you are using Xamarin.Forms, you can make a custom interface

namespace Your.Namespace.Interfaces
{
    public interface IStorageInterface
    {
        double GetFreeSpace(); //Not sure about the return type, try long, or double

    }
}

In your iOS Project:

[assembly: Xamarin.Forms.Dependency(typeof(Your.Namespace.iOS.StorageRenderer))]
namespace Your.Namespace.iOS
{
    public class StorageRenderer : IStorageInterface
    {
        public double GetFreeSpace()
        {
            return NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;
        }

    }
}

Upvotes: 4

Related Questions