您的位置:首页 > 其它

WP7 Isolated Storage 系列 - 2.创建文件夹和文件

2013-01-28 15:17 323 查看
这是第二篇关于系列短文章“WP7 Isolated Storage系列”,专注于真实实用,并且有源代码的例子,而不是存粹理论。接下来我将要讨论关于在Windows Phone 7 Isolated Storage中创建文件夹和文件。

·
WP7 Isolated Storage 系列 - 1.Isolated Storage简介

·
WP7 Isolated Storage 系列 - 2.创建文件夹和文件

·
WP7 Isolated Storage 系列 - 3.使用IsolatedStorageSettings存储数据

·
WP7 Isolated Storage 系列 - 4.读取和存储文本文件

·
WP7 Isolated Storage 系列 - 5.使用XmlSerializer读取和存储XML文件

·
WP7 Isolated Storage 系列 - 6.使用XmlWriter读取和存储XML文件

·
WP7 Isolated Storage 系列 - 7.读取和存储图像

·
WP7 Isolated Storage 系列 - 8.读取和存储拍摄的图像

·
WP7 Isolated Storage 系列 - 9.读取和存储二进制文件

·
WP7 Isolated Storage 系列 - 10.文件操作

·
WP7 Isolated Storage 系列 - 11.建议和最佳实践

·
WP7 Isolated Storage 系列 - 12.开源数据库和帮助库文件

让我们从创建一个简单的Windows Phone 7工程开始。下一步需要添加下面的namespaces到MainPage.xaml.cs中去(或者你可以在另一个页面中使用这个代码):

using System.IO;
using System.IO.IsolatedStorage;

在WP7 Isolated Storage中的文件夹

· 创建文件夹

为了创建一个名为“New Folder”的文件夹,我们只需要调用类IsolatedStorageFile的方法CreateDirectory:

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
myIsolatedStorage.CreateDirectory("NewFolder");

注意:你也可以创建除了“New Folder”以外的合成路径文件夹,比如“Folder1/Folder2/Folder3/NewFolder”。

myIsolatedStorage.CreateDirectory("Folder1/Folder2/Folder3/NewFolder");

· 删除文件夹

为了删除一个文件夹你只需要调用类IsolatedStorageFile方法DeleteDirectory:

myIsolatedStorage.DeleteDirectory("NewFolder");

· 最佳实践

-总是检查这个目录是否存在

myIsolatedStorage.DirectoryExists(directoryName)

-在使用Isolated Storage的时候总是使用try{}catch{},可以预防用户看到不被期望的exception。下面的这个方法就是通过参数文件夹名来创建文件夹:

public void CreateDirectory(string directoryName)
{
try
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if(!string.IsNullOrEmpty(directoryName) && !myIsolatedStorage.DirectoryExists(directoryName))
{
myIsolatedStorage.CreateDirectory(directoryName);
}
}
catch (Exception ex)
{
// handle the exception
}
}

创建一个文件夹只需要调用这个方法并且赋予要求的参数:

this.CreateDirectory("NewFolder");

-此外也可以使用相同的检查来删除文件夹:

注意:在被删除之前文件夹必须是空的。一旦删除之后,文件夹将不能被恢复。

public void DeleteDirectory(string directoryName)
{
try
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!string.IsNullOrEmpty(directoryName) && myIsolatedStorage.DirectoryExists(directoryName))
{
myIsolatedStorage.DeleteDirectory(directoryName);
}
}
catch (Exception ex)
{
// handle the exception
}
}

为了删除文件夹只需要调用这个方法:

this.DeleteDirectory("NewFolder");

注意:当前使用Isolated Storage并不能够重命名一个文件夹。

在WP7 Isolated Storage中的文件

· 创建文件

为了创建一个名为“SomeFile.txt”的文件,只需要使用StreamWriter:

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("NewFolder\\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage));

注意:如果你没有指定任何文件夹,那个这个文件将会被创建在根目录下面。同样你也可以使用IsolatedStorageFileStream来创建文件:

IsolatedStorageFileStream stream1 = new IsolatedStorageFileStream("SomeTextFile.txt", FileMode.Create, myIsolatedStorage);




· 删除文件

为了从Isolated Storage中删除文件,只需要调用类IsolatedStorageFile的DeleteFile方法:

myIsolatedStorage.DeleteFile("NewFolder/SomeFile.txt");

· 最佳实践

-总是检查我们即将创建的文件所在的文件夹是否存在

myIsolatedStorage.DirectoryExists(directoryName)

-总是检查我们即将创建的文件是否存在。如果存在,你必须要删除存在的文件然后创建一个新的。

myIsolatedStorage.FileExists(filePath)

-在使用Isolated Storage的时候总是使用try{}catch{},可以预防用户看到不被期望的exception。下面的这个方法就是通过参数文件夹名来创建文件夹:

try
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writeFile;
if (!myIsolatedStorage.DirectoryExists("NewFolder"))
{
myIsolatedStorage.CreateDirectory("NewFolder");
writeFile = new StreamWriter(new IsolatedStorageFileStream("NewFolder\\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage));
}
else
{
writeFile = new StreamWriter(new IsolatedStorageFileStream("NewFolder\\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage));
}
}
catch (Exception ex)
{
// do something with exception
}

注意:如果要完全删除所有的数据在Isolated Storage中,只需要调用store.Remove(),这里的“store”是IsolatedStorageFile的一个实例。

在这篇文件中我讨论了关于在WP7 Isolated Storage中如何创建文件夹和文件。敬请期待接下来的文章。

原文链接:http://www.windowsphonegeek.com/tips/all-about-wp7-isolated-storage-files-and-folders
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: