Windows多级目录创建API代码

xingyun86 2020-8-26 996

Windows多级目录创建API代码

//判断目录是否存在,若不存在则创建
__inline static 
BOOL CreateCascadeDirectory(LPCSTR lpPathName, //Directory name
	LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL  // Security attribute
)
{
	CHAR* pToken = NULL;
	CHAR czPathTemp[MAX_PATH] = { 0 };
	CHAR czPathName[MAX_PATH] = { 0 };
	strcpy(czPathName, lpPathName);
	pToken = strtok(czPathName, ("\\"));
	while (pToken)
	{
		sprintf(czPathTemp, ("%s%s\\"), czPathTemp, pToken);
		//创建失败时还应删除已创建的上层目录,此次略
		if (!CreateDirectoryA(czPathTemp, lpSecurityAttributes))
		{
			DWORD dwError = GetLastError();
			if (dwError != ERROR_ALREADY_EXISTS && dwError != ERROR_ACCESS_DENIED)
			{
				return FALSE;
			}
		}
		pToken = strtok(NULL, ("\\"));
	}
	return TRUE;
}


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回