CString strFolderPath="c:\\test"
// 判断路径是否存在
if (!PathIsDirectory(m_strFolderPath) )
{
CString strMsg;
strMsg.Format ("指定路径\"%s\"不存在,是否创建?", m_strFolderPath);
if (AfxMessageBox(strMsg, MB_YESNO) == IDYES)
{
if (!CreateDirectory(m_strFolderPath, NULL))//here
{
strMsg.Format ("创建路径\"%s\"失败!是否继续?", m_strFolderPath);
if (AfxMessageBox(strMsg, MB_YESNO) == IDYES)
return;
}
}
}
其实就是一个函数:CreateDirectory
代码如下:
GetModuleFileName( NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH ); //NULL 返回应用程序的全路径 strPath.ReleaseBuffer(); //清空缓冲区 strPath = strPath.Left(strPath.ReverseFind(_T('\\'))); //获取整个路径
str = strPath + "\\"+ sDate + "-" + sTime; //文件夹名称
CString csSubString;
while(-1 != str.Find("\\"))
{
int nPos = str.Find("\\");
csSubString += str.Left(nPos + 1);
str = str.Right(str.GetLength() - nPos -1);
CreateDirectory(csSubString,NULL);
}
csSubString += str;
CreateDirectory(csSubString,NULL); //创建子目录