Q
創建目錄+子目錄
28
A
回答
56
Directory.CreateDirectory(@ 「C:\匹配\上傳」)將整理出這一切爲您服務。你不需要創建所有的子目錄!創建目錄方法爲您創建所有目錄和子目錄。
8
if (!System.IO.Directory.Exists(@"C:\Match\Upload"))
{
System.IO.Directory.CreateDirectory(@"C:\Match\Upload");
}
+1
目錄可能不存在於if中,但在使用該方法創建嘗試期間仍然存在。不要打擾存在和使用捕獲。 – 2009-11-05 14:32:43
+7
即使目錄存在,實際調用CreateDirectory也不會失敗,因此使用它是多餘的。 – RichardOD 2011-11-30 13:31:26
1
爲Google員工:在純粹的Win32/C++,使用SHCreateDirectoryEx
inline void EnsureDirExists(const std::wstring& fullDirPath)
{
HWND hwnd = NULL;
const SECURITY_ATTRIBUTES *psa = NULL;
int retval = SHCreateDirectoryEx(hwnd, fullDirPath.c_str(), psa);
if (retval == ERROR_SUCCESS || retval == ERROR_FILE_EXISTS || retval == ERROR_ALREADY_EXISTS)
return; //success
throw boost::str(boost::wformat(L"Error accessing directory path: %1%; win32 error code: %2%")
% fullDirPath
% boost::lexical_cast<std::wstring>(retval));
//TODO *djg* must do error handling here, this can fail for permissions and that sort of thing
}
0
這是一個帶DirectoryInfo
對象,將創建目錄和所有子目錄的例子:
var path = @"C:\Foo\Bar";
new System.IO.DirectoryInfo(path).Create();
呼叫如果路徑已經存在,則Create()
不會出錯。
如果它是一個文件路徑,你可以這樣做:
var path = @"C:\Foo\Bar\jazzhands.txt";
new System.IO.FileInfo(path).Directory.Create();
相關問題
- 1. 爲子目錄創建Makefile目錄
- 2. 創建admin子目錄
- 3. 創建目錄
- 4. 創建目錄
- 5. .htaccess子目錄子目錄
- 6. 過濾分支篩選出目錄下創建子目錄
- 7. 如何用子目錄創建多個目錄?
- 8. 在IIS 6.0的子目錄中創建虛擬目錄(編程)
- 9. Android創建目錄
- 10. PHP創建目錄
- 11. codeigniter創建目錄
- 12. PHP:創建目錄
- 13. java:創建目錄
- 14. Python:創建目錄
- 15. 目錄未創建
- 16. 創建目錄Python
- 17. 在WPF中創建子目錄?
- 18. 如何創建幾個子目錄
- 19. FTP客戶端子目錄創建
- 20. playframework創建動態子目錄
- 21. rsync創建缺席葉子目錄
- 22. GNU make在新創建的子目錄
- 23. Url重定向子目錄子目錄
- 24. 走子目錄Python從子目錄
- 25. .htaccess /子目錄/ - > /子目錄/索引
- 26. Makefile來建立子目錄
- 27. 虛擬目錄創建
- 28. 二郎創建目錄
- 29. 創建jar文件目錄?
- 30. 創建git開發目錄
參考https://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory.aspx – 2015-12-09 15:01:25