I'm working on a Windows Phone 7 app, and I was wondering whether anyone had a definitive answer on whether or not I have to check if a directory exists before creating one, and what the advantages/disadvantages of doing/not doing so are. As far as I can tell, from stepping through my code, the following two blocks of code work in the same manner:
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
if (!appStorage.DirectoryExists(sDirectory))
{
appStorage.CreateDirectory(sDirectory);
}
}
and
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
appStorage.CreateDirectory(sDirectory);
}
Is it safe to use the second block of code? It didn't seem to throw an exception if the directory already existed, and also seemed to leave the contents of the directory alone.