微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight 2 Beta 2的Isolated Storage

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

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,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","[email protected]");
//Read appSetting
string mailAddress = (string)appSettings["email"];
//Change existing appSetting
appSettings["email"] = "[email protected]";
//and finally delete it...
appSettings.Remove("email");
关于存储的更详细内容,可参看的
在silverlight中使用IsolateStore隔离存储(上)
@L_404_5@
可以通过代码去调整存储空间的大小,使用的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";        }    }}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐