Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

c# - How I can find out the available free space in a universal Windows Phone 8.1 App

does anyone know how to access the available free space in a universal Windows Phone 8.1 App? In a Windows Phone 8(.1) Silverlight App I could use this Code:

int availableStorage = IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace;

But System.IO.IsolatedStorage isn't available in a Windows (Phone) 8.1 App.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It may be done like in answers to this question. As I've tried, the method like in the code below returns the number of free bytes:

public async Task<UInt64> GetFreeSpace()
{
    StorageFolder local = ApplicationData.Current.LocalFolder;
    var retrivedProperties = await local.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace" });
    return (UInt64)retrivedProperties["System.FreeSpace"];
}

// usage:
UInt64 myFreeSpace = await GetFreeSpace();

More about porperties to retrive (their format etc.) you can find at MSDN.


Some more information - note that method gets free space of a folder it's reffering to. So if we run it like this:

public async Task<UInt64> GetFreeSpace(StorageFolder folder)
{
    var retrivedProperties = await folder.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace" });
    return (UInt64)retrivedProperties["System.FreeSpace"];
}

// and use it like this:
UInt64 spaceOfInstallationFolder = await GetFreeSpace(ApplicationData.Current.LocalFolder);
UInt64 spaceOfMusicLibrary = await GetFreeSpace(KnownFolders.MusicLibrary);

We will get results dependant on Settings of the User's Phone:

  • ApplicationData.Current.LocalFolder reffers to a place where the App was installed - so if User had set that Apps will be installed on SD, then you will see free place on SD card,
  • KnownFolders.MusicLibrary (for example, it can be PicturesLibrary and so on, also ensure that you added cappabilities in your manifest file, otherwise you will get an exception) - the same situation dependand on User settings - it can be space on Phone or SD

So if the App is installed on Phone, then reffering to LocalFolder you will get space on Phone. If you want space on SD then you can for example run method like this (remember about capabilities):

UInt64 spaceOfMusicLibrary = await GetFreeSpace((await KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault());

Note also that getting free space of the Phone in case User had set everything to be installed on SD (Apps, Music, Pictures) is useless as you won't be able to use it (unauthorized access). Simply - if you have an access to a folder you can get its available space.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...