Silverlight 2 Beta 2的Isolated Storage

2018-01-31 11:32:00 浏览数 (1)

Silverlight beta 2 的配置有一个重大变化就是对DRM 和Application Storage的配置

Application storage的默认大小是1M,可以通过代码修改,通过使用IsolatedStorageFile 类操作。IsolatedStorageFile 类抽象了isolated storage的虚拟文件系统 . 你创建一个 IsolatedStorageFile 类的实例, 你可以使用它对文件或文件夹进行列举或管理.同样你还可以使用该类的 IsolatedStorageFileStream 对象来管理文件内容.

虚拟文件系统根目录是对于每个机器当前登陆用户不同的, 它是一个隐藏的文件夹,存在于物理文件系统中. 每个application的不同标识将会使其映射到不同的文件夹中, 也就是说,将分配给每个不同的application 一个属于它的虚拟文件系统. .NET Framework version 2.0中的文件夹节构和隐藏架构同样在 .NET Framework for Silverlight中也用到了.

var store = IsolatedStorageFile.GetUserStoreForApplication()

代码语言:js复制
store.CreateDirectory("MyApp1");
string subdirectory1 = System.IO.Path.Combine("MyApp1", "SubDir1");
store.CreateDirectory(subdirectory1);
IsolatedStorageFileStream rootFile = store.CreateFile("InTheRoot.txt");
rootFile.Close();

//Write to text file
string filePath = System.IO.Path.Combine(subdirectory1, "MyApp1A.txt");
try
{
   using (StreamWriter sw =
            new StreamWriter(store.OpenFile(filePath,
            FileMode.Open, FileAccess.Write)))
            {
                sw.WriteLine("To do list:");
                sw.WriteLine("1. Buy supplies.");
             }
}
catch (IsolatedStorageException ex)
{

    sb.AppendLine(ex.Message);
}

//Read from text file
try
{
    using (StreamReader reader =
        new StreamReader(store.OpenFile(filePath,
            FileMode.Open, FileAccess.Read)))
    {
        string contents = reader.ReadToEnd();
        sb.AppendLine(filePath   " contents:");
        sb.AppendLine(contents);
    }
}
catch (IsolatedStorageException ex)
{

    sb.AppendLine(ex.Message);
}

使用Application storage存储数据,要使用IsolatedStorageSettings.ApplicationSettings

代码语言:javascript复制
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

现在可以用它来读取,修改、删除应用程序配置:

代码语言:javascript复制
//Add new appSetting
appSettings.Add("email", "alexg@sela.co.il");
//Read appSetting
string mailAddress = (string)appSettings["email"];
//Change existing appSetting
appSettings["email"] = "alex@somemail.com";
//and finally delete it...
appSettings.Remove("email");

关于存储的更详细内容,可参看的

在silverlight中使用IsolateStore隔离存储(上)

在silverlight中使用IsolateStore隔离存储(下)

可以通过代码去调整存储空间的大小,使用的API是IncreaseQuotaTo()函数。

代码如下:

代码语言:js复制
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    // Request 5MB more space in bytes.
    Int64 spaceToAdd = 5242880;
    Int64 curAvail = store.AvailableFreeSpace;  
    // If available space is less than
    // what is requested, try to increase.
    if (curAvail < spaceToAdd)
    {  
        // Request more quota space.
        if (!store.IncreaseQuotaTo(store.Quota   spaceToAdd))
        {
            // The user clicked NO to the
            // host's prompt to approve the quota increase.
            tbResults.Text = "User declined to approve Quota inrease";
        }
        else
        {
            // The user clicked YES to the
            // host's prompt to approve the quota increase.
            tbResults.Text = "Quota inreased";
        }
    }
}

0 人点赞