前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Silverlight 2 Beta 2的Isolated Storage

Silverlight 2 Beta 2的Isolated Storage

作者头像
张善友
发布2018-01-31 11:32:00
4860
发布2018-01-31 11:32:00
举报

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

silverlightcfg
silverlightcfg

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

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

var store = IsolatedStorageFile.GetUserStoreForApplication()

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

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

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

//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()函数。

代码如下:

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";
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2008-06-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档