尝试删除独立存储中的目录时出现异常 [英] Exception when trying to delete a directory in Isolated Storage

查看:23
本文介绍了尝试删除独立存储中的目录时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试删除 Windows Phone 7 独立存储中的目录时,出现以下异常:

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('/'));
}

注意事项:

  1. 将其放入 try-catch 将隐藏异常,但仍然不会删除目录!
  2. 在调用之前,我使用 DeleteFile() 删除了其中的所有文件,因此问题与目录中的现有文件无关.
  3. 修剪目录名称是为了确保它是一个有效的目录名称.

有什么想法吗?

谢谢.

推荐答案

好的,问题解决了,问题是文件没有被正确删除.我感到困惑的原因是,当您删除无效文件时,IsolatedStorageFile 类不会警告您.这是正确的代码和一些注意事项:

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('\\'));
}

注意事项:

  1. 文件路径中的\"和/"没有区别
  2. 删除目录时需要trimEnd(),否则会抛出异常路径必须是有效的文件名".
  3. GetFileNames() 和 GetDirectoryNames() 仅返回名称部分,而不是完整路径.因此,为了使用每个结果,您需要将其与目录(在本例中为 DeleteFile())结合起来

这篇关于尝试删除独立存储中的目录时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆