1

In IsolatedStorage you have to delete all the folders and files inside a directory before you can delete the directory itself in IsolatedStorage.

Normally If I'm deleting a directory in IsolatedStorage which has some files inside I would get the list of directories, then use a foreach statement and check if each of those has files then use another foreach statement to delete each of the files inside those directories.

However I have a much more complicated FileSystem going on in IsolatedStorage which looks a bit like this:

Several Main directories which contain Several sub-directories these sub-directories contain another 1-100 additional sub-directories which contain about 3-5 files

At the moment the only technique I know of (using foreach statements and many IsolatedStorageFile.GetUserStoreForApplication().GetDirectoryNames()) is hardly what you would call efficient.

Is there an easier/easy way of checking for recursively deleting directories and their files?

2 Answers 2

4

Since the API does not support recursive deletions, so you wlll have to do it yourself. Like e.g.

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);
}
3
  • That's sort of the thing I was using before, but it still doesn't solve the issue of deleting directories inside directories inside directories? Aug 25, 2013 at 9:48
  • 1
    @user2696648 uhm ... yes it does. it deletes them recursively by calling DeleteDirectoryRecursively on the subdirectories it finds.
    – Alex
    Aug 25, 2013 at 15:11
  • thanks, I set String dirName = "" in the params as I wanted to delete everything.
    – Valamas
    Dec 17, 2013 at 1:03
0

The user store has a method Clear() that cleans the whole thing

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

Keep in mind this deletes EVERYTHING, even configurations

3
  • Yeah I know about that, I want to be able to delete specific directories which have several sub directories which contain more directories and more files Aug 25, 2013 at 9:49
  • Then recursivity it is... And that you already have it, don't you?
    – Gonzix
    Aug 25, 2013 at 11:25
  • 1
    IsolatedStorageFile has no Clear() method, it has a Remove() method. Which does the same thing.
    – Jesse
    Jan 31, 2014 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.