問題描述
我正在嘗試創(chuàng)建一個函數,該函數采用目錄名稱(C:fooar
或 ..fooar..az
或 \someserverfooar
),并根據需要創(chuàng)建目錄,以便創(chuàng)建整個路徑.
I'm trying to create a function that takes the name of a directory (C:fooar
, or ..fooar..az
, or \someserverfooar
), and creates directories as necessary so that the whole path is created.
我自己正在嘗試一個非常幼稚的實現,這似乎是一個字符串處理的噩夢.有 /
vs ,有一種以
\
開頭的網絡共享的特殊情況(你也不能嘗試 mkdir() 路徑的前兩級是機器名和共享名),并且有 .
類型的廢話可以存在于路徑中.
I am attempting a pretty naive implementation of this myself and it seems to be a string processing nightmare. There is /
vs , there is the special case of network shares which begin with
\
(also you can't attempt to mkdir() the first two levels of the path which are machine name and share name), and there is .
type nonsense that can exist in a path.
在 C++ 中是否有一種簡單的方法可以做到這一點?
Does there exist a simple way to do this in C++?
推薦答案
如果您不需要支持 Windows 2000 之前的 Windows 版本,您可以使用 SHCreateDirectoryEx 函數.考慮一下:
If you don't need to support Windows versions prior to Windows 2000, you can use the SHCreateDirectoryEx function for this. Consider this:
int createDirectoryRecursively( LPCTSTR path )
{
return SHCreateDirectoryEx( NULL, path, NULL );
}
// ...
if ( createDirectoryRecursively( T("C:\Foo\Bar\Baz") ) == ERROR_SUCCESS ) {
// Bingo!
}
如果使用這樣的 shell32.dll API 成為問題,您總是可以使用其他東西(可能是手動循環(huán))重新實現上面的 createDirectoryRecursively 函數.
In case using such shell32.dll API ever becomes an issue, you can always reimplement the createDirectoryRecursively function above with something else (possibly a hand-wired loop).
這篇關于如何在 Win32 中遞歸創(chuàng)建文件夾?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!