2013-03-13 60 views
1
string path1 = @"C:\temp\"; 
string path2 = ""; 

// Create directory temp if it doesn't exist 
if (!Directory.Exists(path1)) 
{ 
    Directory.CreateDirectory(path1); 
} 

我創建了上述目錄temp,但我卻不知道如何在temp創建子目錄(如temp1)?在WPF中創建子目錄?

+1

你已經有了path1的目錄,所以只需調用'CreateDirectory(path1 +「[sub folder here]」)'...? – DGibbs 2013-03-13 15:09:31

+1

請注意,您實際上並不需要'Exists()'檢查。 – SLaks 2013-03-13 15:09:56

+0

這可能是一個奇怪的問題,但這個問題究竟與WPF有關? – 2013-03-13 15:19:31

回答

4

你已經有了基本的代碼,你只需要稍微調整它。據對CreateDirectory

任何文檔和路徑指定的所有目錄的創建,除非它們已經存在或除非路徑的某些部分是無效的。

所以你可以指定完整路徑到temp1並使用一個調用。

string path1 = @"C:\temp\"; 
string path2 = Path.Combine(path1, "temp1"); 

// Create directory temp1 if it doesn't exist 
Directory.CreateDirectory(path2); 

請注意,這適用於任何時候你想創建一個目錄。在WPF應用程序中做這件事沒什麼特別的。

+1

請注意,您實際上並不需要父'CreateDirectory()'檢查。 – SLaks 2013-03-13 15:10:17

+0

@SLaks謝謝,你是對的。直到我查看了文檔,我並不是100%肯定的。 – 2013-03-13 15:13:39