Can I know how many remaining storage space in the phone when the app was installed to SD?

StackOverflow https://stackoverflow.com/questions/23652635

  •  22-07-2023
  •  | 
  •  

Pergunta

If my app was installed to SD by user, and in Storage Sense, pictures, music, videos, apps and even download stuff all were set to store in SD, can I know how many remaining storage space in the phone?

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("start time:" + DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss:fff"));

        /* Install folder */
        StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        IReadOnlyList<StorageFolder> installFolderList = await installFolder.GetFoldersAsync();
        Debug.WriteLine("installFolderList:" + installFolderList.Count);
        IReadOnlyList<StorageFile> installFileList = await installFolder.GetFilesAsync();
        Debug.WriteLine("installFileList:" + installFileList.Count);
        ulong installSize = await GetFreeSpace(installFolder);
        Debug.WriteLine("installSize:" + installSize / 1024 / 1024 / 1024);

        /* Local folder */
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        IReadOnlyList<StorageFolder> localFolderList = await localFolder.GetFoldersAsync();
        Debug.WriteLine("localFolderList:" + localFolderList.Count);
        IReadOnlyList<StorageFile> localFileList = await localFolder.GetFilesAsync();
        Debug.WriteLine("localFileList:" + localFileList.Count);
        ulong localSize = await GetFreeSpace(localFolder);
        Debug.WriteLine("localSize:" + localSize / 1024 / 1024 / 1024);

        /* Local cache folder */
        StorageFolder localCacheFolder = ApplicationData.Current.LocalCacheFolder;
        IReadOnlyList<StorageFolder> localCacheFolderList = await localCacheFolder.GetFoldersAsync();
        Debug.WriteLine("localCacheFolderList:" + localCacheFolderList.Count);
        IReadOnlyList<StorageFile> localCacheFileList = await localCacheFolder.GetFilesAsync();
        Debug.WriteLine("localCacheFileList:" + localCacheFileList.Count);
        ulong localCacheSize = await GetFreeSpace(localCacheFolder);
        Debug.WriteLine("localCacheSize:" + localCacheSize / 1024 / 1024 / 1024);

        /* Temporary folder */
        StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
        IReadOnlyList<StorageFolder> tempFolderList = await tempFolder.GetFoldersAsync();
        Debug.WriteLine("tempFolderList:" + tempFolderList.Count);
        IReadOnlyList<StorageFile> tempFileList = await tempFolder.GetFilesAsync();
        Debug.WriteLine("tempFileList:" + tempFileList.Count);
        ulong tempSize = await GetFreeSpace(tempFolder);
        Debug.WriteLine("tempSize:" + tempSize / 1024 / 1024 / 1024);

        /* Picture folder */
        StorageFolder picFolder = KnownFolders.PicturesLibrary;
        IReadOnlyList<StorageFolder> picFolderList = await picFolder.GetFoldersAsync();
        Debug.WriteLine("picFolderList:" + picFolderList.Count);
        IReadOnlyList<StorageFile> picFileList = await picFolder.GetFilesAsync();
        Debug.WriteLine("picFileList:" + picFileList.Count);
        ulong picSize = await GetFreeSpace(picFolder);
        Debug.WriteLine("picSize:" + picSize / 1024 / 1024 / 1024);

        /* CameraRoll folder */
        StorageFolder cameraRollFolder = KnownFolders.CameraRoll;
        IReadOnlyList<StorageFolder> cameraRollFolderList = await cameraRollFolder.GetFoldersAsync();
        Debug.WriteLine("cameraRollFolderList:" + cameraRollFolderList.Count);
        IReadOnlyList<StorageFile> cameraRollFileList = await cameraRollFolder.GetFilesAsync();
        Debug.WriteLine("cameraRollFileList:" + cameraRollFileList.Count);
        ulong cameraRollSize = await GetFreeSpace(cameraRollFolder);
        Debug.WriteLine("cameraRollSize:" + cameraRollSize / 1024 / 1024 / 1024);

        /* Music folder */
        StorageFolder musicFolder = KnownFolders.MusicLibrary;
        IReadOnlyList<StorageFolder> musicFolderList = await musicFolder.GetFoldersAsync();
        Debug.WriteLine("musicFolderList:" + musicFolderList.Count);
        IReadOnlyList<StorageFile> musicFileList = await musicFolder.GetFilesAsync();
        Debug.WriteLine("musicFileList:" + musicFileList.Count);
        ulong musicSize = await GetFreeSpace(musicFolder);
        Debug.WriteLine("musicSize:" + musicSize / 1024 / 1024 / 1024);

        /* Videos folder */
        StorageFolder videosFolder = KnownFolders.VideosLibrary;
        IReadOnlyList<StorageFolder> videoFolderList = await videosFolder.GetFoldersAsync();
        Debug.WriteLine("videoFolderList:" + videoFolderList.Count);
        IReadOnlyList<StorageFile> videoFileList = await videosFolder.GetFilesAsync();
        Debug.WriteLine("videoFileList:" + videoFileList.Count);
        ulong videosSize = await GetFreeSpace(videosFolder);
        Debug.WriteLine("videosSize:" + videosSize / 1024 / 1024 / 1024);

        Debug.WriteLine("end time:" + DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss:fff"));
    }

    public static async Task<ulong> GetFreeSpace(IStorageItem sf)
    {
        var properties = await sf.GetBasicPropertiesAsync();
        var filteredProperties = await properties.RetrievePropertiesAsync(new[] { "System.FreeSpace" });
        var freeSpace = filteredProperties["System.FreeSpace"];
        return (ulong)freeSpace;
    }
Foi útil?

Solução

Making an answer from the comment above:

Enumerate the pictures library, there should be 2 CameraRoll folders one internal and one external. Use

StorageFolder.Path

and check if the path starts with C:\ to see if the folder is the internal or external one as shown here.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top