MFC 创建多级文件目录

char* pPath;
int iFlag;
CString sTempPath;

pPath = CShToolKit::WideChToMultiByte(strPicPath); //CString  转 char *
iFlag=_access(pPath,0);
if (iFlag == -1) //目录不存在
{  
    CreateMultipleDirectory(strPicPath);
}


bool CMainAppModule::CreateMultipleDirectory(const CString& szPath)

{
    CString strDir(szPath);//存放要创建的目录字符串
    //确保以'\'结尾以创建最后一个目录
    if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
    {
        strDir.AppendChar(_T('\\'));
    }
    std::vector<CString> vPath;//存放每一层目录字符串
    CString strTemp;//一个临时变量,存放目录字符串
    bool bSuccess = false;//成功标志
    //遍历要创建的字符串
    for (int i=0;i<strDir.GetLength();++i)
    {
        if (strDir.GetAt(i) != _T('\\'))
        {//如果当前字符不是'\\'
            strTemp.AppendChar(strDir.GetAt(i));
        }
        else
        {//如果当前字符是'\\'
            vPath.push_back(strTemp);//将当前层的字符串添加到数组中
            strTemp.AppendChar(_T('\\'));
        }
    }

    //遍历存放目录的数组,创建每层目录
    std::vector<CString>::const_iterator vIter;
    for (vIter = vPath.begin(); vIter != vPath.end(); vIter++)
    {
        //如果CreateDirectory执行成功,返回true,否则返回false
        bSuccess = CreateDirectory(*vIter, NULL) ? true : false;    
    }

    return bSuccess;
}

// Unicode 下CString --> char*转换
char* CShToolKit::WideChToMultiByte(CString string)
{
    // 注意:以下n和len的值大小不同,n是按字符计算的,len是按字节计算的
    int nChar = string.GetLength();
    int len = WideCharToMultiByte(CP_ACP,0,string,string.GetLength(),NULL,0,NULL,NULL);

    // 为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小
    char* pbuf = new char[len+1];     //以字节为单位

    // 宽字节编码转换成多字节编码
    WideCharToMultiByte(CP_ACP,0,string,string.GetLength(),pbuf,len,NULL,NULL);
    pbuf[len] = '\0';   //多字节字符以'\0'结束
    return pbuf;
}


// Unicode 下char* --> CString转换
CString CShToolKit::MultiByteToWideCh(char* pBuf)
{
    // 声明标识符
    USES_CONVERSION;
    return CString(A2W(pBuf));
}

// 目录进行格式化    strDirectory 格式: [E:\directory]
CString CShToolKit::FormatDirectory(CString strDirectory)
{
    // 定义目录字符串
    CString directory(strDirectory);

    // 首先先在目录尾部增加\字符
    directory.Append(_T("\\"));

    // 然后将字符串中的所有[\]换成 [\\]
    directory.Replace(_T("\\"), _T("\\\\"));
    return directory;
}

CString CShToolKit::reFormatDirectory(CString strDirectory)
{
    // 定义目录字符串
    CString directory(strDirectory);

    //// 首先先在目录尾部增加\字符
    //directory.Append(_T("\\"));

    // 然后将字符串中的所有[\\]换成 [\]
    directory.Replace(_T("\\\\"), _T("\\"));
    return directory;
}


// UTF-8 转换成 Unicode char
char* CShToolKit::ConvertUtf8ToUnicode(char* pUtf8)
{
    int i = MultiByteToWideChar(CP_UTF8,0,(char*)pUtf8,-1,NULL,0);

    WCHAR* strA = new WCHAR[i];     
    MultiByteToWideChar(CP_UTF8,0,(char*)pUtf8,-1,strA,i);
    i = WideCharToMultiByte(CP_ACP,0,strA,-1,NULL,0,NULL,NULL);

    char* strB = new char[i];
    WideCharToMultiByte(CP_ACP,0,strA,-1,strB,i,NULL,NULL);

    //strB即为所求     
    delete []strA;     
    return strB;
}

void CShToolKit::ConvertUtf8ToGBK(CString& strUtf8)
{
    USES_CONVERSION;

    int len=MultiByteToWideChar(CP_UTF8, 0, CW2A(strUtf8), -1, NULL,0);
    unsigned short * wszGBK = new unsigned short[len+1];
    memset(wszGBK, 0, len * 2 + 2);
    MultiByteToWideChar(CP_UTF8, 0, CW2A(strUtf8), -1, (LPWSTR)wszGBK, len);

    len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
    char *szGBK=new char[len + 1];
    memset(szGBK, 0, len + 1);
    WideCharToMultiByte (CP_ACP, 0, (LPWSTR)wszGBK, -1, szGBK, len, NULL,NULL);

    strUtf8 = szGBK;
    delete[] szGBK;
    delete[] wszGBK;
}

// 将CString 转换为 DWORD
DWORD CShToolKit::ConvertCToD(CString string)
{
    return DWORD(_tstol(string));
}

CString CShToolKit::ConvertDToC(DWORD dWord)
{
    CString strRet;
    strRet.Format(_T("%ld"), dWord);
    return strRet;
}

// 将int 转换为 CString
CString CShToolKit::ConvertToInt(int value)
{
    CString string;
    string.Format(_T("%d"), value);
    return string;
}


猜你喜欢

转载自blog.csdn.net/woxiangzi/article/details/50995699
今日推荐