星空网 > 软件开发 > 操作系统

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主要代码

Windows  phone开发之文件夹与文件操作系列(一)文件夹与文件操作images/loading.gif' data-original="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" />Windows  phone开发之文件夹与文件操作系列(一)文件夹与文件操作
 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     else21      {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

Windows  phone开发之文件夹与文件操作系列(一)文件夹与文件操作Windows  phone开发之文件夹与文件操作系列(一)文件夹与文件操作
 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    else21    {22     MessageBox.Show("文件不存在");23     }24 25   }26  }27 28 //写入文件29 void WriteButton_Click(object sender, RoutedEventArgs e)30 {31  try32       {33         using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())34         {35           //打开文件36           using (IsolatedStorageFileStream FileStream = file.OpenFile(FileNameTextBox.Text + ".txt", FileMode.Open, FileAccess.Write))37           {38             //实例化StreamWriter类39             StreamWriter streamWriter = new StreamWriter(FileStream);40             //使用WriteLine方法使用41             streamWriter.WriteLine(ContentTextBox.Text);42             //写入完成后需要关闭43             streamWriter.Close();44           }45         }46 47       }48       catch (IsolatedStorageException ex)49       {50         MessageBox.Show(ex.ToString());51 52       }53     }54 55 56 //读取文件57 void ReadFilePage_Loaded(object sender, RoutedEventArgs e)58 {59  using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())60   {61   if (file.FileExists(NavigationContext.QueryString["file"].ToString()))62    {63    //打开文件64    using (IsolatedStorageFileStream FileStream = file.OpenFile(NavigationContext.QueryString["file"].ToString(), FileMode.Open, FileAccess.ReadWrite))65     {         66       StreamReader streamReader = new StreamReader(FileStream);   //实例化streamReader类    67       this.ContentTextBlock.Text = streamReader.ReadLine(); //使用ReadToEnd()方法读取内容68       streamReader.Close();//关闭文件流69      }70    }71   else72    {73    MessageBox.Show(NavigationContext.QueryString["file"].ToString() + "文件不存在");74    }75   }76 }

View Code


 




原标题:Windows phone开发之文件夹与文件操作系列(一)文件夹与文件操作

关键词:Windows

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流