文件的隔离存储

   隔离存储(Isolated Storage)提供一种存储应用程序相关数据的机制,可以将这些数据存储在不同的叔叔分隔区(Data Compartment)。存储不限于类型,如组件
标识符、程序创建者信息等,类似于INI文件,不同的是隔离存储的数据可以根据不同的用户、组件或者安全级别存放,避免复杂的文件管理及应用程序数据冲突。数据分隔区通常管理到一至数个隔离存储文件(称为Store),其中包含数据存储的实际目录位置。使用类IsolatedStorageFile的一些方法可以创建或返回特定的存储文件(Store)。静态方法GetStore()用来创建与特定应用程序相关的Isolated Storage文件实例对象。其定义如下:

             

  public static IsolatedStorageFile GetStore(

       IsolatedStorageScope scope,

        Object domainIdentity,

       Object assemblyIdentity

    )

 

参数

scope

类型:System.IO.IsolatedStorage.IsolatedStorageScope

IsolatedStorageScope 值的按位组合。

domainIdentity

类型:System.Object

Object,包含应用程序域标识的证据。

assemblyIdentity

类型:System.Object

Object,包含代码程序集标识的证据。

返回值

类型:System.IO.IsolatedStorage.IsolatedStorageFile

表示参数的 IsolatedStorageFile

第一个参数指定这个文件的相关应用范围,如下表

None

未使用独立存储。

User

通过用户标识限制范围的独立存储。

Domain

范围限制在应用程序域标识内的独立存储(应用于指定的应用程序域)。

Assembly

范围限制在程序集的标识内的独立存储。(应用于指定的组件)

Roaming

独立存储区可以放置在文件系统中可以漫游的位置中(指定隔离存储放置于指定的文件系统)(如果漫游用户数据已在基础操作系统上启用)

Machine

范围限制在机器内的独立存储。

Application

范围限制在应用程序内的独立存储。

可以使用|组合所需的枚举值。

第二个参数指定所属的应用程序域,null表示当前的应用程序域。

第三个参数表示应用程序组件,null表示当前的应用程序组件。

     IsolatedStorageFile类还提供另一个实例方法CreateDirectory(),用于为指定的隔离存储创建目录结构,创建完成后和普通的文件目录一样可以访问,如果要取得其中的目录名称,可以直接使用GetDirectoryNames()方法。创建目录后就可以在其中创建文件了,这时用到另外一个类IsolatedStorageFileStream。我们可以通
过此类的构造函数

public IsolatedStorageFileStream(
	string path,
	FileMode mode,
	IsolatedStorageFile isf
)
来指向一个文件。第一个参数是文件完整的路径名称,第二个参数指定这个文件是新创建还是打开,第三个参数为IsolatedStorageFile类的实例,指定文件要加
入的隔离存储。文件存在以后,我们可以使用IsolatedStorageFile类的GetFileNames()方法获得文件列表,此方法需要字符串类型一个目录路径作为参数。
具体内容我们可以参考以下实例:
案例1:创建一个应用程序,并创建与其相关的隔离存储文件(Store),在其中创建目录和文件,按照结构列出所有的目录和文件名称并输出的控制台然后删除根目录
下的文件。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.IO.IsolatedStorage;

 

namespace ConsoleApplication1

{

    class Storage

    {

        static void Main(string[] args)

        {

            //使用GetStore()方法创建与应用程序相关的Isolated Storage文件实例对象

            IsolatedStorageFile store = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

            //创建关联文件

            IsolatedStorageFileStream fs = new IsolatedStorageFileStream("file1.bin", FileMode.Create, store);

 

            IsolatedStorageFileStream fs1 = new IsolatedStorageFileStream("file2.txt", FileMode.Create, store);

 

            IsolatedStorageFileStream fs2 = new IsolatedStorageFileStream("file3.txt", FileMode.Create, store);

 

            //创建目录

            store.CreateDirectory("ISDRoot");

            store.CreateDirectory("ISDRoot2");

            store.CreateDirectory("ISDRoot//SubISDRoot");

            store.CreateDirectory("ISDRoot3");

 

            //在目录下创建文件

            IsolatedStorageFileStream fs3 = new IsolatedStorageFileStream("ISDRoot//file.txt", FileMode.Create, store);

            IsolatedStorageFileStream fs4 = new IsolatedStorageFileStream("ISDRoot//SubISDRoot//file1.txt", FileMode.Create, store);

            IsolatedStorageFileStream fs5 = new IsolatedStorageFileStream("ISDRoot//SubISDRoot//file12.txt", FileMode.Create, store);

            IsolatedStorageFileStream fs6 = new IsolatedStorageFileStream("ISDRoot//SubISDRoot//file3.txt", FileMode.Create, store);

            IsolatedStorageFileStream fs7 = new IsolatedStorageFileStream("ISDRoot3//file4.txt", FileMode.Create, store);

 

            //关闭文件连接

            fs.Close();

            fs1.Close();

            fs2.Close();

            fs3.Close();

            fs4.Close();

            fs5.Close();

            fs6.Close();

            fs7.Close();

 

            Console.WriteLine("列出根目录下扩展名为.bin的文件");

            foreach (string fileName in store.GetFileNames("*.bin"))

            {

                Console.WriteLine(fileName);

            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("列出根目录下扩展名为.txt的文件");

            foreach (string fileName in store.GetFileNames(".txt"))

            {

                Console.WriteLine(fileName);

            }

            Console.WriteLine("-------------------------------------------");

 

            Console.WriteLine("列出根目录下所有相关文件");

            foreach (string fileName in store.GetFileNames("*"))

            {

                Console.WriteLine(fileName);

            }

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine("列出所有目录下的所有文件");

            foreach (string dir in store.GetDirectoryNames("*"))

            {

                Console.WriteLine(dir);

                foreach (string fileName in store.GetFileNames(dir + "/*"))

                {

                    Console.Write("\t"+fileName);

                }

                foreach (string sDir in store.GetDirectoryNames(dir + "/*"))

                {

                    Console.WriteLine(dir + "/" + sDir);

                    foreach (string fName in store.GetFileNames(dir + "/" + sDir + "/*"))

                    {

                        Console.WriteLine(fName);

                    }

                }

            }

            //删除根目录下的文件

            store.DeleteFile("file1.bin");

            store.DeleteFile("file2.txt");

            store.DeleteFile("file3.txt");

            //删除目录

            store.DeleteDirectory("ISDRoot2");

            Console.ReadLine();

        }

      

    }

}

 创建好文件后就可以进行读写了,如下例:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.IO.IsolatedStorage;

 

namespace ConsoleApplication1

{

    class Storage

    {

        static void Main(string[] args)

        {

            //使用GetStore()方法创建与应用程序相关的Isolated Storage文件实例对象

            IsolatedStorageFile store = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

            //创建关联文件

            using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream("file1.bin", FileMode.Create, store))

            {

                using (StreamWriter sw = new StreamWriter(fs))

                {

                    Console.WriteLine("开始写入数据");

                    sw.WriteLine("程序编写:程之恒");

                }

             

            }

            using (IsolatedStorageFileStream fs1 = new IsolatedStorageFileStream("file1.bin", FileMode.Open, store))

            {

                using (StreamReader sr = new StreamReader(fs1))

                {

                    Console.WriteLine("开始读取数据");

                    Console.WriteLine(sr.ReadLine());

                }

            }

            Console.ReadLine();

        }

      

    }

}

posted @ 2010-08-03 19:02  坐看风起  阅读(2891)  评论(0编辑  收藏  举报