创建和删除文件夹。

hesiemen 2005-07-16 02:03:08
我做了一个函数创建文件夹并删除该文件夹,虽然创建成功但是删除不行。代码如下:
CString strPath = _T("C:\\Temp");
SECURITY_ATTRIBUTES psa;
GET_FILEEX_INFO_LEVELS fInfoLevelId;
LPVOID lpFileInformation;
SHCreateDirectoryEx(NULL,strPath,NULL);
//GetFileAttributesEx(strPath,fInfoLevelId,lpFileInformation);
SHFILEOPSTRUCT fos;
ZeroMemory(&fos,sizeof(fos));
//fos.hwnd=HWND_DESKTOP;
fos.hwnd = NULL;
fos.wFunc=FO_DELETE;
fos.fFlags=FOF_SILENT | FOF_NOCONFIRMATION |~FOF_ALLOWUNDO |FOF_NOERRORUI;



fos.pFrom=strPath;
fos.pTo =NULL;
fos.lpszProgressTitle = _T("");
fos.fAnyOperationsAborted = TRUE;
//删除文件夹及其内容
if(0!=SHFileOperation(&fos))
return;

我发现文件夹的属性是只读的。
...全文
247 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
CQP 2005-07-19
  • 打赏
  • 举报
回复
extern "C" __declspec(dllexport) bool WINAPI DeleteDirectory(char *DirName)
{
CFileFind tempFind;
char cTempFileFind[200];
sprintf(cTempFileFind,"%s\\*.*",DirName);
BOOL IsFinded=(BOOL)tempFind.FindFile(cTempFileFind);
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile();
if(!tempFind.IsDots())
{
char foundFileName[200];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
if(tempFind.IsDirectory())
{
char cTempDir[200];
sprintf(cTempDir,"%s\\%s",DirName,foundFileName);
DeleteDirectory(cTempDir);
}
else
{
char cTempFileName[200];
sprintf(cTempFileName,"%s\\%s",DirName,foundFileName);
DeleteFile(cTempFileName);
}
}
}
tempFind.Close();
return TRUE;
}
djfu 2005-07-19
  • 打赏
  • 举报
回复
哈哈,那叫删除文件夹么?

删除文件夹就是要删除文件夹下面的所有的文件、包括子文件夹。
hifan 2005-07-17
  • 打赏
  • 举报
回复
CreateDirectory();
RemoveDirectory();

不过删除的时候文件夹一定要为空!!!
yzkzero 2005-07-17
  • 打赏
  • 举报
回复
不行的话
CString strPath = _T("C:\\Temp");
strPath.AppendChar(0);
strPath.AppendChar(0);

pFrom
Address of a buffer to specify one or more source file names. These names must be fully qualified paths. Standard Microsoft MS-DOS wild cards, such as "*", are permitted in the file-name position. Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of pFrom.
yzkzero 2005-07-17
  • 打赏
  • 举报
回复
CString strPath = _T("C:\\Temp");
strPath.AppendChar(0);
xing_xing_xing 2005-07-16
  • 打赏
  • 举报
回复
可以工作
SHCreateDirectoryEx(NULL,"c:\\temp",NULL);
SHFILEOPSTRUCT fo;
memset(&fo,0,sizeof(fo));
fo.wFunc = FO_DELETE;
fo.pFrom = "c:\\temp";
fo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION |~FOF_ALLOWUNDO |FOF_NOERRORUI;
SHFileOperation(&fo);
flypigluo 2005-07-16
  • 打赏
  • 举报
回复
简单点的就这么做
//-----------------------------
char csFolderPath[] = {"d:\\myfolder"};
::CreateDirectory(csFolderPath,NULL);
::RemoveDirectory(csFolderPath);
//-----------
要其他属性的把NULL改了

16,551

社区成员

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

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

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