如何新建多级目录

blp 2003-08-18 06:38:40
有没有直接的方法可以建立多级目录

用CreateDirectory函数好像只能在已经存在的目录新建目录。

...全文
318 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fbmsf 2003-08-20
  • 打赏
  • 举报
回复
ImageHlp.h
blp 2003-08-19
  • 打赏
  • 举报
回复
fbmsf(FBM)
你好!
那要包含那個頭文件呢。
akiko 2003-08-18
  • 打赏
  • 举报
回复
MakeSureDirectoryPathExists("c:\\aa\\bb\\");是可以的.
SHCreateDirectory看来是.Net的函数
GFK 2003-08-18
  • 打赏
  • 举报
回复
软件环境:
winxp+sp1
Microsoft Visual Studio .NET 2003

#include <windows.h>
#include <shlobj.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{

if (ERROR_SUCCESS == SHCreateDirectory(0, L"d:\\ddd\\ddd\\dddd\\ddddd"))
{
MessageBox(0, 0, 0, 0);
}

return 0;
}
shines77 2003-08-18
  • 打赏
  • 举报
回复
同意楼上的,DirPath末尾必需是 \ 字符
fbmsf 2003-08-18
  • 打赏
  • 举报
回复
The MakeSureDirectoryPathExists function creates all the directories in the specified DirPath.

BOOL MakeSureDirectoryPathExists(

IN LPSTR DirPath
);
abcicq 2003-08-18
  • 打赏
  • 举报
回复
//////////////////////////////////////////////////////////////////////////////////////
//*
//* 功能:
//* 创建多层目录.
//*
//* 参数说明:
//*lpPathName:要创建的目录名.必须以"\"结尾,否则最后一个"\"后面部分将视为文件名.
//* eg. C:\\abc ----> C:\\(abc is a file name)
//* C:\\abc\\ --> C:\\abc(abc is a directory name)
//*lpSecurityAttributes: Pointer to a SECURITY_ATTRIBUTES structure. If
//*lpSecurityAttributes is NULL,the directory gets a default security descriptor.
//*chDefaultDriver:创建的驱动器. If chDriver is NULL,chDriver gets a default C.
//*
//////////////////////////////////////////////////////////////////////////////////////
BOOL CreateMutiDirectory(LPCTSTR lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes,TCHAR chDefaultDriver)
{
CString strPathName = lpPathName;
if(0 == strPathName.GetLength())
return TRUE;

if(-1 == strPathName.Find(_T(':')))
{
strPathName.Insert(0,_T(":\\"));
strPathName.Insert(0,chDefaultDriver);//* eg. Only abc --> C:\\abc
}

int iPosFirst = strPathName.Find(_T('\\'));
int iTSize = sizeof(TCHAR);
int iPosSecond;
if(-1 == (iPosSecond = strPathName.Find(_T('\\'),iPosFirst + iTSize)))
return TRUE; //* eg. Only C:\abc
if(iPosFirst + iTSize == iPosSecond)
{
iPosFirst = iPosSecond;
if(-1 == (iPosSecond = strPathName.Find(_T('\\'),iPosFirst + iTSize)))
return TRUE; //* eg. X:\\abc X:\\ is net driver.
}

TCHAR szPath[MAX_PATH];

while(-1 != iPosSecond)
{
iPosFirst = iPosSecond;
//* szPath end by '\'
memcpy(szPath,(LPCTSTR)strPathName,iPosFirst+sizeof(TCHAR));
//* szPath end by '\0' in here.
szPath[iPosFirst+sizeof(TCHAR)] = _T('\0');
if(!::CreateDirectory(szPath,lpSecurityAttributes))
{
if(0xB7 != GetLastError()) //* Have exist.
return FALSE;
}
iPosSecond = strPathName.Find(_T('\\'),iPosFirst+sizeof(TCHAR));
}
return TRUE;
}
zhouyong0371 2003-08-18
  • 打赏
  • 举报
回复
是呀,自己封装一个函数,然后要创建的时候把新路径传进去,在里边还是要一级一级的创建。
寻开心 2003-08-18
  • 打赏
  • 举报
回复
www.codeproject.com上面有例子,可以自动创建多级的
孤必有邻 2003-08-18
  • 打赏
  • 举报
回复
好象没有,只好自己一级级创建了

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧