在 IsolatedStorage 中,您必须先删除目录中的所有文件夹和文件,然后才能在 IsolatedStorage 中删除目录本身。
通常,如果我要删除 IsolatedStorage 中的一个目录,其中有一些文件,我会得到目录列表,然后使用 foreach 语句并检查每个目录是否都有文件,然后使用另一个 foreach 语句删除这些目录中的每个文件。
但是我在 IsolatedStorage 中有一个更复杂的文件系统,看起来有点像这样:
Several Main directories 其中包含 Several sub-directories 这些 sub-directories 包含另外 1-100 个额外的子目录,其中包含大约 3-5 个文件
目前我所知道的唯一技术(使用 foreach 语句和许多 IsolatedStorageFile.GetUserStoreForApplication().GetDirectoryNames())很难称为高效。
是否有更简单的方法来检查递归删除目录及其文件?
请您参考如下方法:
由于API不支持递归删除,所以需要自己动手。比如
public static void DeleteDirectoryRecursively(this IsolatedStorageFile storageFile, String dirName)
{
String pattern = dirName + @"\*";
String[] files = storageFile.GetFileNames(pattern);
foreach (var fName in files)
{
storageFile.DeleteFile(Path.Combine(dirName, fName));
}
String[] dirs = storageFile.GetDirectoryNames(pattern);
foreach (var dName in dirs)
{
DeleteDirectoryRecursively(storageFile, Path.Combine(dirName, dName));
}
storageFile.DeleteDirectory(dirName);
}
