设为首页 加入收藏

TOP

Windows phone开发之文件夹与文件操作系列(一)文件夹与文件操作(一)
2017-10-11 16:06:01 】 浏览:7655
Tags:Windows phone 开发 文件夹 文件 操作 系列

Windows phone7中文件的存储模式是独立的,即独立存储空间(IsolatedStorage)。对文件夹与文件操作,需要借助IsolatedStorageFile类。 IsolatedStorageFile提供了对独立存储的空间获取,文件夹的删除、移动,文件的创建、删除等IO操作。 在Windows phone7中对文件的操作,都需要引入命名空间System.IO.IsolatedStorage和System.IO。

在System.IO.IsolatedStorage 命名空间下有以下几种类: (详细了解:https://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage%28VS.95%29.aspx)

   1.IsolatedStorageFile 类 表示包含文件和文件夹的独立存储区,用于操控独立存储空间文件夹和文件。

  2.IsolatedStorageFileStream 类 表示公开独立存储中的文件,用于读写操控独立存储空间里的文件流。

  3.IsolatedStorageSettings 类 提供一个在独立存储中存储键/值对的 Dictionary<TKey, TValue>,用于存储应用程序的配置信息的Dictionary。

  4.IsolatedStorageException 类 用于检测独立存储中的操作失败时所引发的异常。

在Windows phone7中对文件的操作一般有以下几个步骤:

  1.首先引入命名空间System.IO.IsolatedStorage和System.IO;

  2.获取应用程序的独立存储空间,调用静态方法GetUserStoreForApplication()返回IsolatedStorageFile对象;

  3.利用获取的独立空间对象提供的方法进行IO操作(如果涉及文件流操作,应在文件流操作结束后将文件流关闭);

  4.对文件操作出现异常进行捕获。

文件夹与文件操作 对文件夹与文件的操作基于IsolatedStorageFile 类对象,常用方法有:

  CopyFile(String, String):将现有文件复制到新文件。

  CopyFile(String, String, Boolean):将现有文件复制到新文件,还可以覆盖现有文件。

  CreateDirectory:在独立存储范围中创建目录。

  CreateFile:在独立存储区中创建文件。

  DeleteDirectory:删除独立存储范围中的目录。

  DeleteFile:删除独立存储区中的文件。

  DirectoryExists:检查指定的路径是否指的是独立存储区中的现有目录。

  FileExists:检查指定的路径是否指的是独立存储区中的现有文件。

  MoveDirectory:将指定的目录及其内容移到新位置。

  MoveFile:将指定文件移到新位置,还可以允许您指定新文件名。

  OpenFile(String, FileMode): 在指定的模式中打开文件。

  OpenFile(String, FileMode, FileAccess):以指定的文件访问权限在指定的模式下打开文件。

  其中在进行写入文件操作时,操作稍微复杂一些。 文件的写入是以流的方式写入的,进行写入操作时首先用IsolatedStorage提供的IsolatedStorageFileStream 文件流操作类打开该文件; 然后再使用StreamWriter类将打开的文件对对象进行数据写入;最后关闭文件流。

在文件的读取操作和文件的写入步骤基本相同,使用StreamReader类进行读取,最后也是需要关闭文件流。

下面通过例子了解文件夹与文件操作实现过程

文件夹操作:

   MainPage.xaml.cs主要代码

 1 //创建文件夹
 2 void CreateButton_Click(object sender, RoutedEventArgs e)
 3 {
 4   using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 5   {
 6     file.CreateDirectory(dir);
 7    }
 8  }
 9 
10 
11  //查检文件夹
12 void ExistsButton_Click(object sender, RoutedEventArgs e)
13 {
14    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
15     {
16       if (file.DirectoryExists(dir))
17         {
18            MessageBox.Show("文件夹存在!");
19          }
20         else
21          {
22             MessageBox.Show("文件夹不存在!");
23           }
24  }
25 
26 
27 //删除文件夹
28 void DeleteButton_Click(object sender, RoutedEventArgs e)
29 {
30    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
31    {
32       file.DeleteDirectory(dir);
33     }
34  }
View Code

文件操作:
  MainPage.xaml.cs

 1 //新建文件
 2 void NewButton_Click(object sender, RoutedEventArgs e)
 3 {          
 4     using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 5      {
 6       IsolatedStorageFileStream FileStream= file.CreateFile(textBox.Text + ".txt");
 7        //关闭文件流
 8        FileStream.Close();     
 9       }
10 }
11 //检查文件
12 void CheckButton_Click(object sender, RoutedEventArgs e)
13 {
14    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
15    {
16      if (file.FileExists(textBox.Text + ".txt"))
17       {
18         MessageBox.Show("文件已经存在");
19       }
20       else
21       {
22          MessageBox.Show("文件不存在");
23        }
24 
25     }
26  }
27 
28  //写入文件
29 void WriteButton_Click(object sender, Rou
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇02.移动先行之谁主沉浮----第一个.. 下一篇02.移动先行之谁主沉浮----第一个..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目