Zero Code

零度代码

使用系统的 SDK 创建多级目录

 

 1#include <shlobj.h>
 2#include <string>
 3
 4#pragma comment(lib, "shell32.lib")
 5
 6BOOL CreateDir(LPCTSTR pszDirPath)
 7{
 8#if defined(UNICODE) || defined(_UNICODE)
 9    std::wstring strDirPath = pszDirPath;
10#else
11    std::string strDirPath = pszDirPath;
12#endif
13
14    if (strDirPath.empty())
15        return FALSE;
16
17    TCHAR szPreFix[] = _T("\\\\?\\");
18    if (0 != strDirPath.find(szPreFix))
19    {    
20        strDirPath = szPreFix;
21        strDirPath += pszDirPath;
22    }

23
24    INT nErrorCode = SHCreateDirectoryEx(NULL, strDirPath.c_str(), NULL);
25
26    switch (nErrorCode)
27    {
28    case ERROR_SUCCESS:
29        return TRUE;
30    case ERROR_ALREADY_EXISTS:
31    case ERROR_FILE_EXISTS:
32        if (FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(strDirPath.c_str()))
33            return TRUE;
34    // no default
35    }

36
37    return FALSE;
38}

39
40int _tmain(int argc, _TCHAR* argv[]) 
41{
42    CreateDir(_T("d:\\a\\b\\c\\d\\e\\f"));
43    return 0;
44}

posted on 2011-04-18 01:03 cntrump 阅读(243) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


My Links

Blog Stats

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜