目的:实现用CreateDirectory创建多级目录
1.引入静态库
#include "shlwapi.h"
#pragma comment(lib,"shlwapi.lib")
2.函数
const wchar_t* wcstrrchr(const wchar_t* str, const wchar_t wc)
{
const wchar_t* pwc = NULL;
for (int i=wcslen(str)-1;i>=0;i--)
{
if (str[i] == wc)
{
pwc = str + i;
break;
}
}
return pwc;
}
bool createMultiDir(const wchar_t* path)
{
if (path == NULL) return false;
const wchar_t* pwcStrrchr = wcstrrchr(path,L‘\\‘);
if (!pwcStrrchr) return false;
if (PathIsDirectory(path)) return true;
wchar_t wsSubPath[MAX_PATH] = {};
memset(wsSubPath,0,sizeof(wsSubPath));
for (int i=0; i<pwcStrrchr-path; i++)
wsSubPath[i] = *(path+i);
createMultiDir(wsSubPath);
if(CreateDirectory(path,NULL)) return true;
return false;
}
3.调用案例
createMultiDir(L"d:\\whish\\test\\好孩子\\宝马\\测试.txt"); //绝对路径
createMultiDir(L".\\whish\\test\\好孩子\\宝马\\测试.txt");//相对路径
原文地址:http://blog.51cto.com/whish/2103950
时间: 2024-10-08 13:54:24