使用英语阅读

通过


如何:预见独立存储中的空间不足条件

使用独立存储的代码受配额限制,配额指定了独立存储文件和目录所在数据隔离舱的大小上限。 该配额由安全策略定义,管理员可以对其进行配置。 如果尝试写入数据时超过了所允许的最大大小,将引发 IsolatedStorageException 异常并使操作失败。 这有助于防止恶意的拒绝服务攻击,此类攻击可能会导致应用因为数据存储已满而拒绝请求。

为有助于确定给定写入尝试是否有可能由于此原因而失败,IsolatedStorage 类提供了下列三个只读属性:AvailableFreeSpaceUsedSizeQuota。 您可以使用这些属性来确定写入存储区是否将导致超过存储区所允许的最大大小。 记住独立存储可能被同时访问;因此,当计算剩余存储量时,该存储空间可能在您尝试写入存储区时已被使用。 但是,您可以使用存储区的最大大小来确定是否将达到可用存储的上限。

Quota 属性依赖程序集中的证据来正常工作。 因此,只应在使用 IsolatedStorageFileGetUserStoreForAssemblyGetUserStoreForDomain 方法创建的 GetStore 对象上检索此属性。 以其他任何方式创建的 IsolatedStorageFile 对象(例如从 GetEnumerator 方法返回的对象)将无法返回准确的最大大小。

示例

下面的代码示例获取独立存储,创建一些文件,并检索 AvailableFreeSpace 属性。 剩余空间以字节为单位进行报告。

C#
using System;
using System.IO;
using System.IO.IsolatedStorage;

public class CheckingSpace
{
    public static void Main()
    {
        // Get an isolated store for this assembly and put it into an
        // IsolatedStoreFile object.
        IsolatedStorageFile isoStore =  IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly, null, null);

        // Create a few placeholder files in the isolated store.
        new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
        new IsolatedStorageFileStream("Another.txt", FileMode.Create, isoStore);
        new IsolatedStorageFileStream("AThird.txt", FileMode.Create, isoStore);
        new IsolatedStorageFileStream("AFourth.txt", FileMode.Create, isoStore);
        new IsolatedStorageFileStream("AFifth.txt", FileMode.Create, isoStore);

        Console.WriteLine(isoStore.AvailableFreeSpace + " bytes of space remain in this isolated store.");
    } // End of Main.
}

请参阅