目录不存在,递归创建目录(MFC)

CString GetDirectoryRemoveRightBackSlash(const CString& strPath)
{
    int nIndex = strPath.ReverseFind('\\');
    if (nIndex == -1)
    {
        return CString();
    }
    return strPath.Mid(0, nIndex+1);
}

BOOL CreateDirectoryWithFullPath(const CString& strDirectory)
{
    if (strDirectory.IsEmpty())
    {
        return FALSE;
    }

    CString strPath = strDirectory;
    if (*strDirectory.Right(1) == '\\')
    {
        strPath = strDirectory.Mid(0, strDirectory.GetLength()-1);
    }
    else
    {
        strPath = strDirectory;
    }
    if (!CreateDirectory((LPCSTR)strPath, NULL))
    {
        if (GetLastError() != ERROR_PATH_NOT_FOUND)
        {
            return FALSE;
        }
        else
        {
            if (!CreateDirectoryWithFullPath(GetDirectoryRemoveRightBackSlash(strPath)))    //create remove backslash directory
            {
                return FALSE;
            }
            return CreateDirectory((LPCSTR)strPath, NULL);    //create include backslash directory
        }
    }
    return TRUE;
}

 

posted on 2022-07-21 10:01  wu.g.q  阅读(111)  评论(0编辑  收藏  举报

导航