VC小技巧--创建多级目录

Win32中提供的创建目录的API函数--CreateDirectory只能创建单层目录,下面提供一个创建多级目录的方法:

None.gif bool Utility::CreateMultipleDirectory( const CString & szPath)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifCStringstrDir(szPath);
//存放要创建的目录字符串
InBlock.gif
//确保以'/'结尾以创建最后一个目录
InBlock.gif
if(strDir.GetAt(strDir.GetLength()-1)!=_T('//'))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifstrDir.AppendChar(_T(
'//'));
ExpandedSubBlockEnd.gif}

InBlock.gifstd::vector
<CString>vPath;//存放每一层目录字符串
InBlock.gif
CStringstrTemp;//一个临时变量,存放目录字符串
InBlock.gif
boolbSuccess=false;//成功标志
InBlock.gif
//遍历要创建的字符串
InBlock.gif
for(inti=0;i<strDir.GetLength();++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(strDir.GetAt(i)!=_T('//'))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//如果当前字符不是'//'
InBlock.gif
strTemp.AppendChar(strDir.GetAt(i));
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//如果当前字符是'//'
InBlock.gif
vPath.push_back(strTemp);//将当前层的字符串添加到数组中
InBlock.gif
strTemp.AppendChar(_T('//'));
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

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

InBlock.gif
InBlock.gif
returnbSuccess;
ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值