如何在 Win32 中递归创建文件夹?

How do I recursively create a folder in Win32?(如何在 Win32 中递归创建文件夹?)
本文介绍了如何在 Win32 中递归创建文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试创建一个函数,该函数采用目录名称(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 中递归创建文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent class inheritance in C++(防止 C++ 中的类继承)
Why should I declare a virtual destructor for an abstract class in C++?(为什么要在 C++ 中为抽象类声明虚拟析构函数?)
Why is Default constructor called in virtual inheritance?(为什么在虚拟继承中调用默认构造函数?)
C++ cast to derived class(C++ 转换为派生类)
C++ virtual function return type(C++虚函数返回类型)
Is there any real risk to deriving from the C++ STL containers?(从 C++ STL 容器派生是否有任何真正的风险?)