如何在通用Windows Phone 8.1应用程序中查找可用空间 [英] How I can find out the available free space in a universal Windows Phone 8.1 App

查看:86
本文介绍了如何在通用Windows Phone 8.1应用程序中查找可用空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在通用Windows Phone 8.1应用程序中访问可用空间吗?在Windows Phone 8(.1)Silverlight应用程序中,我可以使用以下代码:

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;

但是System.IO.IsolatedStorage在Windows(电话)8.1应用程序中不可用。

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

推荐答案

可以像在答案中那样这个问题。正如我尝试过的那样,下面的代码中的方法将返回可用字节数:

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();

有关要检索的文件的更多信息(其格式等),您可以在MSDN上找到

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 会返回到安装应用程序的位置-因此,如果用户将应用程序设置为安装在SD上,然后您会看到SD卡上的可用空间

  • KnownFolders.MusicLibrary (例如,可以是PicturesLibrary和等等,还请确保您已在清单文件中添加了功能,否则会出现异常)-相同的情况取决于用户设置-电话或SD上的空间可以是

  • 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

因此,如果该应用程序安装在Phone上,则使用 LocalFolder ,您将在Phone上获得空间。如果要在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());

还请注意,如果用户将所有内容都设置为要安装在SD上,则可以获得电话的可用空间(应用,音乐,图片)无效,因为您将无法使用它(未经授权的访问)。很简单-如果您有权访问文件夹,则可以获取其可用空间。

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.

这篇关于如何在通用Windows Phone 8.1应用程序中查找可用空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆