前一段时间,写个程序,要创建一个多级目录。MFC中的CreateDirectory()函数只能够创建一级目录,所以我就编了个创建多级目录的函数。
函数很简单,直接调用就可以了。
![](http://820808.blog.51cto.com/editor/icons/etc_20.gif)
函数说明:
入口:要创建的目录,CString类型
出口:BOOL类型,true 成功,false 失败。
代码如下:
CreateMuliteCategory(CString FilePath)
{
CString Tempstr,dir;
Tempstr="";
int index=Tempstr.Find('\\');
while(!SetCurrentDirectory(FilePath))//ÅжÏÎļþ¼ÐÊÇ·ñ´æÔÚ
{
index ++;
while('\\' != FilePath.GetAt(index))
{
index ++;
}
CString s;
s.Format("%d",index);
Tempstr = FilePath.Left(index);//µÃµ½²¿·Ö·¾¶
CreateDirectory(Tempstr,NULL);
}
if(!SetCurrentDirectory(FilePath))
{
MessageBox("Îļþ¼Ð´´½¨Ê§°Ü!");
return false;
}
return true;
}
{
CString Tempstr,dir;
Tempstr="";
int index=Tempstr.Find('\\');
while(!SetCurrentDirectory(FilePath))//ÅжÏÎļþ¼ÐÊÇ·ñ´æÔÚ
{
index ++;
while('\\' != FilePath.GetAt(index))
{
index ++;
}
CString s;
s.Format("%d",index);
Tempstr = FilePath.Left(index);//µÃµ½²¿·Ö·¾¶
CreateDirectory(Tempstr,NULL);
}
if(!SetCurrentDirectory(FilePath))
{
MessageBox("Îļþ¼Ð´´½¨Ê§°Ü!");
return false;
}
return true;
}
说明:函数没有采用异常处理,可以根据需要自行修改。此函数只是提供了一个解决办法。
转载于:https://blog.51cto.com/338558/68205