您的位置:首页 > 其它

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

2015-11-10 10:36 495 查看
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主要代码

//新建文件
void NewButton_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream FileStream= file.CreateFile(textBox.Text + ".txt");
//关闭文件流
FileStream.Close();
}
}
//检查文件
void CheckButton_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (file.FileExists(textBox.Text + ".txt"))
{
MessageBox.Show("文件已经存在");
}
else
{
MessageBox.Show("文件不存在");
}

}
}

//写入文件
void WriteButton_Click(object sender, RoutedEventArgs e)
{
try
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
//打开文件
using (IsolatedStorageFileStream FileStream = file.OpenFile(FileNameTextBox.Text + ".txt", FileMode.Open, FileAccess.Write))
{
//实例化StreamWriter类
StreamWriter streamWriter = new StreamWriter(FileStream);
//使用WriteLine方法使用
streamWriter.WriteLine(ContentTextBox.Text);
//写入完成后需要关闭
streamWriter.Close();
}
}

}
catch (IsolatedStorageException ex)
{
MessageBox.Show(ex.ToString());

}
}

//读取文件
void ReadFilePage_Loaded(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (file.FileExists(NavigationContext.QueryString["file"].ToString()))
{
//打开文件
using (IsolatedStorageFileStream FileStream = file.OpenFile(NavigationContext.QueryString["file"].ToString(), FileMode.Open, FileAccess.ReadWrite))
{
StreamReader streamReader = new StreamReader(FileStream);     //实例化streamReader类
this.ContentTextBlock.Text = streamReader.ReadLine();  //使用ReadToEnd()方法读取内容
streamReader.Close();//关闭文件流
}
}
else
{
MessageBox.Show(NavigationContext.QueryString["file"].ToString() + "文件不存在");
}
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: