Mo Valipour
Mo Valipour

Reputation: 13496

Exception when trying to delete a directory in Isolated Storage

I get the following exception when I try to delete a directory in Isolated Storage in Windows Phone 7:

An error occurred while accessing IsolatedStorage.
there is no inner exception.

using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    isf.DeleteDirectory(dir.TrimEnd('/'));
}

Notes:

  1. putting it in a try-catch will hide the exception but still directory is not deleted!
  2. before calling this I delete all files inside that using DeleteFile() so the problem can not be related to existing files inside the directory.
  3. trimming the directory name is to make sure it's a valid directory name.

Any idea?

Thanks.

Upvotes: 1

Views: 2610

Answers (5)

jimagic
jimagic

Reputation: 31

    `public static void DeleteDirectoryRecursive(string directory, IsolatedStorageFile store)
    {
        if (!store.DirectoryExists(directory))
            return;
        var pattern = Path.Combine(directory, "*");
        foreach (var file in store.GetFileNames(pattern))
        {
            store.DeleteFile(Path.Combine(directory, file));
        }
        foreach (var folder in store.GetDirectoryNames(pattern))
        {
             DeleteDirectoryRecursive(Path.Combine(directory, folder), store);
        }

        store.DeleteDirectory(directory);
    }`

Upvotes: 1

Vitalii Vasylenko
Vitalii Vasylenko

Reputation: 4896

Grabbed Valipour's version and make it work. Added some checks to improve stability + fixed some names. This works for me on Lumia 920.

private void DeleteDirectoryRecursive(string dir)
    {
        if (String.IsNullOrEmpty(dir)) return;

        try
        {
            using (var isoFiles = IsolatedStorageFile.GetUserStoreForApplication())
            {
                foreach (var file in isoFiles.GetFileNames(dir + "\\*"))
                {
                    var filename = dir + "/" + file;
                    if (isoFiles.FileExists(filename))
                        isoFiles.DeleteFile(filename);
                }

                foreach (var subdir in isoFiles.GetDirectoryNames(dir))
                {
                    var dirname = dir + subdir + "\\";
                    if (isoFiles.DirectoryExists(dirname))
                        DeleteDirectoryRecursive(dirname);
                }

                var currentDirname = dir.TrimEnd('\\');
                if (isoFiles.DirectoryExists(currentDirname))
                    isoFiles.DeleteDirectory(currentDirname);
            }
        }
        catch (Exception e)
        {
            throw;
        }
    }

Upvotes: 0

onmyway133
onmyway133

Reputation: 48095

Thanks to valipour, I solved the problem

foreach (var file in isf.GetFileNames(dir))
{
    isf.DeleteFile(dir + file);
}

In my case the variable dir is "images". In order to get all file names in "images" directory, you should use isf.GetFileNames("images/*")

Upvotes: 0

Mo Valipour
Mo Valipour

Reputation: 13496

Ok, problem solved, problem was that files were not being deleted correctly. The reason I was confused is that IsolatedStorageFile class does not warn you when you are deleting an invalid file. here is the correct code and some notes:

public static void DeleteDirectoryRecursive(this IsolatedStorageFile isf, string dir)
{
    foreach (var file in isf.GetFileNames(dir))
    {
        isf.DeleteFile(dir + file);
    }

    foreach (var subdir in isf.GetDirectoryNames(dir))
    {
        isf.DeleteDirectoryRecursive(dir + subdir + "\\");
    }

    isf.DeleteDirectory(dir.TrimEnd('\\'));
}

Notes:

  1. there is no difference between '\' and '/' in file paths
  2. trimEnd() is required when DeleteDirectory otherwise exception "path must be a valid file name" is thrown.
  3. GetFileNames() and GetDirectoryNames() return only the name part not the full path. so in order to use each result you need to combine it with the directory (DeleteFile() in this example)

Upvotes: 6

Claus Jørgensen
Claus Jørgensen

Reputation: 26341

According to your code and your description, you would be recreating the IsolatedStorageFile access on every iteration?

You should post all the code, since the error isn't related to what you told so far. As for a working example, see this blog post. If that fails with your directory name, you're clearly doing something wrong.

Also, I believe it uses backslashes, not forward-slashes for paths, so your Trim() would be rather useless either way.

Upvotes: 1

Related Questions