我正在研究Windows Phone 7應用程序,我想知道是否有人對創建一個目錄時是否需要檢查目錄是否存在確定性的答案以及做/不做的優缺點也是。據我所知,從通過我的單步執行代碼,代碼工作的以下兩個塊相同的方式:使用IsolatedStorage,創建它之前是否需要檢查目錄是否存在?
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);
}
是可以安全使用的代碼的第二塊?它似乎沒有拋出異常,如果目錄已經存在,並且似乎只留下目錄的內容。
我發誓之前,我問過這個問題,我一定要錯過它 - 深夜編程的樂趣:) – 2010-09-20 13:19:55
不,你沒有錯。它不在'IsolatedStorageFile.CreateDirectory'文檔中。它在'Directory.CreateDirectory'文檔中。然而'IsolatedStorageFile'調用'Directory'可以被稱爲實現細節並且可以改變。但即使內部發生變化,微軟也必須確保當目錄已經存在時,'IsolatedStorageFile'不會引發異常,因爲這將是一種行爲上的(因此是破壞性的)改變。因此它只是簡單介紹一下,它沒有描述這一點。也許你可以把它報告給微軟。 – Steven 2010-09-20 13:52:53