Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WP7和WP8上递归删除IsolatedStorage中目录的简单方法_C#_Windows Phone 7_Windows Phone 8_Isolatedstorage_Directory - Fatal编程技术网

C# 在WP7和WP8上递归删除IsolatedStorage中目录的简单方法

C# 在WP7和WP8上递归删除IsolatedStorage中目录的简单方法,c#,windows-phone-7,windows-phone-8,isolatedstorage,directory,C#,Windows Phone 7,Windows Phone 8,Isolatedstorage,Directory,在IsolatedStorage中,必须先删除目录中的所有文件夹和文件,然后才能删除IsolatedStorage中的目录本身 通常,如果我在IsolatedStorage中删除一个目录,其中包含一些文件,我会得到目录列表,然后使用foreach语句检查每个目录是否包含文件,然后使用另一个foreach语句删除这些目录中的每个文件 然而,我在IsolatedStorage中有一个复杂得多的文件系统,它看起来有点像这样: 包含多个子目录的几个主目录这些子目录 包含另外1-100个子目录,其中包含大

在IsolatedStorage中,必须先删除目录中的所有文件夹和文件,然后才能删除IsolatedStorage中的目录本身

通常,如果我在IsolatedStorage中删除一个目录,其中包含一些文件,我会得到目录列表,然后使用foreach语句检查每个目录是否包含文件,然后使用另一个foreach语句删除这些目录中的每个文件

然而,我在IsolatedStorage中有一个复杂得多的文件系统,它看起来有点像这样:

包含多个子目录的几个主目录这些子目录 包含另外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);
}

因为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);
}

用户存储区有一个清除整个内容的方法

using (var userStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    userStore.Clear();
}

请记住,这会删除所有内容,即使是配置

用户存储区有一个清除整个内容的方法

using (var userStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    userStore.Clear();
}

请记住,这会删除所有内容,甚至是我以前使用过的配置,但它仍然无法解决删除目录中目录的问题?@user2696648 uhm。。。是的。它通过在找到的子目录上递归调用deleteDirectory来递归删除它们。谢谢,我在参数中设置了String dirName=,因为我想删除所有内容。这是我以前使用的方法,但它仍然不能解决删除目录中目录的问题?@user2696648 uhm。。。是的。它通过在找到的子目录上递归调用deleteDirectory来递归删除它们。谢谢,我在参数中设置了String dirName=,因为我想删除所有内容。是的,我知道这一点,我想能够删除特定的目录,其中有几个子目录,其中包含更多的目录和文件,然后递归它是。。。而且你已经有了它,不是吗?IsolatedStorageFile没有Clear方法,它有一个Remove方法。是的,我知道这一点,我希望能够删除特定的目录,其中有几个子目录,其中包含更多的目录和更多的文件,然后递归它是。。。而且你已经有了它,不是吗?IsolatedStorageFile没有Clear方法,它有一个Remove方法。它做同样的事情。