稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)

稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage),第1张

概述[索引页] [源码下载] 稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage) 作者: webabcd 介绍 Silverlight 2.0 数据的独立存储(Isolated Storage):     IsolatedStorageFile - *** 作 独立存储 的类         IsolatedStorageFile.GetUserStoreF [索引页]
[源码下载]


稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)

作者: webabcd


介绍
Silverlight 2.0 数据的独立存储(Isolated Storage):
    IsolatedStoragefile - *** 作 独立存储 的类
        IsolatedStoragefile.GetUserStoreForSite() - 按站点获取用户的独立存储
        IsolatedStoragefile.GetUserStoreForApplication() - 按应用程序获取用户的独立存储
    IsolatedStorageSettings - 在独立存储中保存的 key-value 字典表
        IsolatedStorageSettings.SiteSettings - 按站点保存的 key-value 字典表
        IsolatedStorageSettings.ApplicationSettings - 按应用程序保存的 key-value 字典表


在线DEMO
http://www.voidcn.com/article/p-ounmxjds-tq.html  


示例
IsolatedStorage.xaml <UserControl x:Class="Silverlight20.Data.IsolatedStorage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel HorizontalAlignment="left">
                <TextBox x:name="txtMsg" margin="5" />
                <TextBox x:name="txtMsg2" margin="5" />
                <button x:name="increase" Content="增加配额" Click="increase_Click" margin="5" />
        </StackPanel>
</UserControl>   IsolatedStorage.xaml.cs

using System;

using System.Collections.Generic;

using System.linq;

using System.Net;

using System.windows;

using System.windows.Controls;

using System.windows.documents;

using System.windows.input;

using System.windows.Media;

using System.windows.Media.Animation;

using System.windows.Shapes;


using System.IO.IsolatedStorage;

using System.IO;


namespace Silverlight20.Data

{

         public partial class IsolatedStorage : UserControl

        {

                 public IsolatedStorage()

                {

                        InitializeComponent();


                         // 演示 IsolatedStoragefile

                        Demo();


                         // 演示 IsolatedStorageSettings

                        Demo2();

                }


                 /// <summary>

                 /// 演示 IsolatedStoragefile

                 /// </summary>

                 voID Demo()

                {

                         // Isolated Storage - 独立存储。一个虚拟文件系统


                         // IsolatedStoragefile - *** 作 独立存储 的类

                         //         IsolatedStoragefile.GetUserStoreForSite() - 按站点获取用户的独立存储

                         //         IsolatedStoragefile.GetUserStoreForApplication() - 按应用程序获取用户的独立存储

                        

                         // using (IsolatedStoragefile isf = IsolatedStoragefile.GetUserStoreForSite())

                         using (IsolatedStoragefile isf = IsolatedStoragefile.GetUserStoreForApplication())

                        {

                                 // DirectoryExists(path) - 指定的路径是否存在

                                 // CreateDirectory(path) - 创建指定的路径

                                 // fileExists(path) - 指定的文件是否存在

                                 // Createfile(path) - 创建指定的文件

                                 // GetDirectorynames() - 获取根目录下的目录名数组

                                 // Getfilenames()() - 获取根目录下的文件名数组

                                 // GetDirectorynames(path) - 获取指定目录下的目录名数组

                                 // Getfilenames(path) - 获取指定目录下的文件名数组

                                 // Openfile() - 打开指定的文件。具体参数参看文档

                                 // Deletefile(path) - 删除指定的文件

                                 // DeleteDirectory(path) - 删除指定的目录(要求目录存在,且目录内无内容)

                                 // Remove() - 关闭 IsolatedStoragefile 对象并移除独立存储内的全部内容



                                 // 在根目录下创建指定的目录

                                 if (!isf.DirectoryExists( "Directory01"))

                                        isf.CreateDirectory( "Directory01");

                                 if (!isf.DirectoryExists( "Directory02"))

                                        isf.CreateDirectory( "Directory02");


                                 // 创建指定的子目录

                                 string subdirectory01 = System.IO.Path.Combine( "Directory01","subdirectory01");

                                 string subdirectory02 = System.IO.Path.Combine( "Directory01","subdirectory02");

                                 if (!isf.DirectoryExists(subdirectory01))

                                        isf.CreateDirectory(subdirectory01);

                                 if (!isf.DirectoryExists(subdirectory02))

                                        isf.CreateDirectory(subdirectory02);

                                


                                 // 根目录下创建指定的文件

                                 if (!isf.fileExists( "Rootfile.txt"))

                                {

                                        IsolatedStoragefileStream isfs = isf.Createfile( "Rootfile01.txt");

                                        isfs.Close();

                                }


                                 // 在指定的目录下创建指定的文件

                                 string file01 = System.IO.Path.Combine(subdirectory01,"file01.txt");

                                 string file02 = System.IO.Path.Combine(subdirectory01,"file02.txt");

                                 string file03 = System.IO.Path.Combine(subdirectory01,"file03.xml");

                                 if (!isf.fileExists(file01))

                                {

                                         // IsolatedStoragefileStream - 独立存储内的文件流。继承自 fileStream

                                        IsolatedStoragefileStream isfs = isf.Createfile(file01);

                                        isfs.Close();

                                }

                                 if (!isf.fileExists(file02))

                                {

                                        IsolatedStoragefileStream isfs = isf.Createfile(file02);

                                        isfs.Close();

                                }

                                 if (!isf.fileExists(file03))

                                {

                                        IsolatedStoragefileStream isfs = isf.Createfile(file03);

                                        isfs.Close();

                                }



                                txtMsg.Text += "根目录下的目录列表:\r\n";

                                 // 获取根目录下的目录名数组

                                 foreach ( string directoryname in isf.GetDirectorynames())

                                {

                                        txtMsg.Text += directoryname + "\r\n";

                                }


                                txtMsg.Text += "根目录下的文件列表:\r\n";

                                 // 获取根目录下的文件名数组

                                 foreach ( string filename in isf.Getfilenames())

                                {

                                        txtMsg.Text += filename + "\r\n";

                                }


                                txtMsg.Text += "目录 Directory01 下的目录列表:\r\n";

                                 // 获取 Directory01 目录下的目录名数组

                                 foreach ( string directoryname in isf.GetDirectorynames(subdirectory01))

                                {

                                        txtMsg.Text += directoryname + "\r\n";

                                }


                                txtMsg.Text += "目录 Directory01/subdirectory01 下的*.txt文件列表:\r\n";

                                 // 获取 Directory01/subdirectory01 目录下的后缀名为 txt 的文件名数组

                                 foreach ( string filename in isf.Getfilenames(System.IO.Path.Combine(subdirectory01,"*.txt")))

                                {

                                        txtMsg.Text += filename + "\r\n";

                                }



                                 if (isf.fileExists(file01))

                                {

                                         // 在文件 file01 中写入内容

                                        IsolatedStoragefileStream streamWrite = isf.Openfile(file01,fileMode.Open,fileAccess.Write);

                                         using (StreamWriter sw = new StreamWriter(streamWrite))

                                        {

                                                sw.Writeline( "我是:webabcd");

                                                sw.Writeline( "我专注于asp.net,Silverlight");

                                        }


                                        txtMsg.Text += "文件 file01.txt 的内容:\r\n";

                                         // 读取文件 file01 中的内容

                                        IsolatedStoragefileStream streamRead = isf.Openfile(file01,fileAccess.Read);

                                         using (StreamReader sr = new StreamReader(streamRead))

                                        {

                                                txtMsg.Text += sr.ReadToEnd();

                                        }

                                }



                                 // 删除文件 file01

                                 if (isf.fileExists(file01))

                                {

                                        isf.Deletefile(file01);

                                }


                                 try

                                {

                                         // 删除目录 subdirectory01

                                        isf.DeleteDirectory(subdirectory01);

                                }

                                 catch (IsolatedStorageException ex)

                                {

                                         // IsolatedStorageException - *** 作临时存储失败时抛出的异常


                                         // 因为 subdirectory01 目录内还有文件,所以会抛异常

                                        txtMsg.Text += ex.Message;

                                }

                        }

                }


                 /// <summary>

                 /// 演示 IsolatedStorageSettings

                 /// </summary>

                 voID Demo2()

                {

                         // IsolatedStorageSettings - 在独立存储中保存的 key-value 字典表

                         //         IsolatedStorageSettings.SiteSettings - 按站点保存的 key-value 字典表

                         //         IsolatedStorageSettings.ApplicationSettings - 按应用程序保存的 key-value 字典表


                        IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;


                         // Add(key,value) - 添加一对 key-value

                        iss.Add( "key","value");

                        txtMsg2.Text += ( string)iss[ "key"] + "\r\n";


                         // 修改指定的 key 的 value

                        iss[ "key"] = "value2";

                        txtMsg2.Text += ( string)iss[ "key"] + "\r\n";


                         // Remove(key) - 移除指定的 key

                         // Count - 字典表内的总的 key-value 数

                        iss.Remove( "key");

                        txtMsg2.Text += iss.Count;

                }

                

                 private voID increase_Click( object sender,RoutedEventArgs e)

                {

                         // 演示独立存储的配额的相关 *** 作

                         using (IsolatedStoragefile isf = IsolatedStoragefile.GetUserStoreForApplication())

                        {

                                 // Quota - 当前配额(KB)

                                 // IncreaseQuotaTo(newQuotaSize) - 增加到指定的配额

                                 // AvailableFreeSpace - 当前的可用配额


                                isf.IncreaseQuotaTo(isf.Quota + 1 * 1024 * 1024);


                                System.windows.browser.@R_404_6832@Page.Window.Alert(

                                         string.Format( "当前配额:{0};可用配额:{1}",isf.Quota,isf.AvailableFreeSpace));

                        }

                }    

        }

}     演示 IsolatedStoragefile 的运行结果:
根目录下的目录列表:
Directory01
Directory02
根目录下的文件列表:
Rootfile01.txt
__LocalSettings
目录 Directory01 下的目录列表:
subdirectory01
目录 Directory01/subdirectory01 下的*.txt文件列表:
file01.txt
file02.txt
文件 file01.txt 的内容:
我是:webabcd
我专注于asp.net,Silverlight
无法删除,目录不为空或不存在。

演示 IsolatedStorageSettings 的运行结果:
value
value2
0


OK
[源码下载]
总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)全部内容,希望文章能够帮你解决稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1052989.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存