需要#include <shlwapi.h>,并且配置好shlwapi.ib
BOOL CreateDirTree( LPCTSTR lpPath )
{
if( NULL == lpPath || _tcslen(lpPath)==0 )
{
return FALSE;
}
if( ::PathFileExists( lpPath) || ::PathIsRoot(lpPath) )
return TRUE;
TCHAR szParentpath[MAX_PATH] = _T("");
::lstrcpy( szParentpath, lpPath );
::PathRemoveBackslash( szParentpath );//去除路径最后的反斜杠
::PathRemoveFileSpec( szParentpath );//将路径末尾的文件名或文件夹和反斜杠去掉
if(0 == _tcscmp(lpPath, szParentpath))
return FALSE;
assert(0 != _tcscmp(lpPath, szParentpath));
if( CreateDirTree( szParentpath) )//递归创建直到上一层存在或是根目录
{
return ::CreateDirectory(lpPath, NULL);
}
else
{
return FALSE;
}
return TRUE;
}