从隔离存储中删除文件而不检查是否存在

Delete file from isolated storage without checking for existance before

我在我们的一个应用程序中看到一些行如下所示:

if (isolatedStore.FileExists(firstFilePath))
    isolatedStore.DeleteFile(firstFilePath);
if (isolatedStore.FileExists(secondFilePath))
    isolatedStore.DeleteFile(secondFilePath);

我是否真的必须在删除之前检查文件是否存在于隔离存储中,或者我可以直接删除它而无需任何功能更改?

不,只需放置一个 try 块,这样就不会出现异常影响您的应用程序。

try { isolatedStore.DeleteFile(filePath); } catch { }

或者您可以检查是否发生错误,并处理异常:

try
{
    isolatedStore.DeleteFile(filePath);
}
catch (IsolatedStorageException ex)
{
    // Handle the exception however you want...
}

您可以删除文件而不检查它们是否存在,只要您准备好处理任何由此产生的异常:来自 How to: Delete Files and Directories in Isolated Storage

An IsolatedStorageException exception is thrown if you try to delete a file or directory that does not exist.