我正在开发一个 Windows Phone 7 应用程序,我想知道是否有人对我是否必须在创建目录之前检查目录是否存在有明确的答案,以及这样做/不这样做的优点/缺点是什么. 据我所知,通过单步执行我的代码,以下两个代码块以相同的方式工作:
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
if (!appStorage.DirectoryExists(sDirectory))
{
appStorage.CreateDirectory(sDirectory);
}
}
和
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
appStorage.CreateDirectory(sDirectory);
}
使用第二个代码块安全吗?如果目录已经存在,它似乎没有抛出异常,并且似乎也单独留下了目录的内容。