簡體   English   中英

Get ios internal storage memory information xamarin forms ios

[英]Get ios internal storage memory information xamarin forms ios

我無法從 iPhone 設備獲得實際的可用存儲空間。 我正在使用此鏈接獲取 xamarin forms ios 中的存儲空間。 以下是我的鏈接代碼。

public double GetRemainingInternalMemoryStorage()
    {
        NSFileSystemAttributes applicationFolder = NSFileManager.DefaultManager.GetFileSystemAttributes(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
        var freeSpace = applicationFolder.FreeSize;
        var totalSpace = applicationFolder.Size;

        
        Console.WriteLine("totalSpace " + freeSpace);
        
        return freeSpace;
    }

我正在開發一個功能,如果存儲空間小於閾值,我需要看到用戶發出警報。 我沒有獲得准確的存儲空間,因此我的功能無法正常工作。

我的設備總共有 32 GB 存儲空間 memory 但是當我檢查上面的代碼時,它看到 31989469184 字節接近 31.98 GB(31989469184/1000/1000/1000),看起來接近正確。但同樣設備的可用空間是 14.2 GB 和上面的代碼它看到了 12259602432 字節,接近 12.25 GB。 我不確定為什么它少了 2 GB。

上面鏈接的 android 代碼運行良好。 如何計算 iOS 中的准確可用空間?

在此處輸入圖像描述

如上所述,iOS 在緩存中保留了一些 memory,我填充了設備的完整 memory,因此設備中只剩下 300 MB 可用空間。 那時我得到了准確的狀態。 因此,我建議如果有人在問題區域中提到功能,請嘗試填充您的 ios 設備,然后進行測試。 以下是我在兩個平台上的最終代碼。

Android代碼

public double GetRemainingInternalMemoryStorage()
    {
        //Using StatFS
        var path = new StatFs(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData));
        long blockSize = path.BlockSizeLong;
        long avaliableBlocks = path.AvailableBlocksLong;
        double freeSpace = blockSize * avaliableBlocks;
        freeSpace = Math.Round(freeSpace / (1024 * 1024), 1); // Converting freespace to mb. Android follows binary pattern, so divide by 1024.
        return freeSpace;
    }

iOS代碼

public double GetRemainingInternalMemoryStorage()
    {
        NSFileSystemAttributes applicationFolder = NSFileManager.DefaultManager.GetFileSystemAttributes(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
        double freeSpace = applicationFolder.FreeSize;

        freeSpace = Math.Round(freeSpace / (1000 * 1000), 1); // Converting freespace to mb. iOS follows decimal pattern, so divide by 1000

        return freeSpace;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM