问题描述
我正在尝试创建一个函数,该函数采用目录名称(C:fooar
或 ..fooar..az
或 \someserverfooar
),并根据需要创建目录,以便创建整个路径.
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 成为问题,您总是可以使用其他东西(可能是手动循环)重新实现上面的 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 中递归创建文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!