Exception when trying to delete a directory in Isolated Storage

北城以北 提交于 2019-12-11 07:04:41

问题


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.


回答1:


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)



回答2:


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.




回答3:


    `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);
    }`



回答4:


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/*")




回答5:


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;
        }
    }


来源:https://stackoverflow.com/questions/6801766/exception-when-trying-to-delete-a-directory-in-isolated-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!