在vc#中如何删除目录下的所有文件?

zxd35 2003-07-30 03:39:43
在vc#中如何删除目录下的所有文件?急急急!!!1
...全文
32 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2003-07-31
  • 打赏
  • 举报
回复
用fxuefei(风) 方法会有问题的,建议用idiotzeng(白痴)
fxuefei 2003-07-31
  • 打赏
  • 举报
回复
//newDir是新创建的目录
DirectoryInfo dir= new DirectoryInfo(newDir);

foreach(FileInfo item in dir.GetFiles())
{
item.Delete();
}
//删除目录
dir.Delete();
happyno7 2003-07-30
  • 打赏
  • 举报
回复
同意楼上
不过,要是你要保留子文件夹,就用
string[] files = System.IO.Directory.GetFiles(directoryName);
foreach(string file in files)
{
System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
System.IO.File.Delete(file);
}
poetc 2003-07-30
  • 打赏
  • 举报
回复
删目录,再建目录呀。
这可比什么删文件要快得多。
我原来就这么干的。
panyee 2003-07-30
  • 打赏
  • 举报
回复
最简单的就是直接删除整个目录, 再建立这个空的目录

Directory.Delete("c:\\123", true);
Directory.CreateDirectory("c:\\123");
xixigongzhu 2003-07-30
  • 打赏
  • 举报
回复
一句话搞定:
Directory.Delete(yourfile, true);
第二个参数如果为True是指删除指定的目录并删除该目录中的任何子目录和文件,注意包括文件。
如果为False(默认)并且这个目录下有子目录和文件就会有异常。
雪狼1234567 2003-07-30
  • 打赏
  • 举报
回复
如下:
File[ ] entries = directory.listFiles( );
for(int i=0; i<entries.length; i++) {
entries[i].delete( );
brightheroes 2003-07-30
  • 打赏
  • 举报
回复
递归
idistudio 2003-07-30
  • 打赏
  • 举报
回复
System.IO.Directory.Delete("D:\Winnt");
jlhdlj 2003-07-30
  • 打赏
  • 举报
回复
http://www.pcfans.com.cn/developer/code/story/0,2000081534,39102247,00.htm
idiotzeng 2003-07-30
  • 打赏
  • 举报
回复
public static void DeleteDirectory(string directoryName)
{
string[] files = System.IO.Directory.GetFiles(directoryName);
foreach(string file in files)
{
System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
System.IO.File.Delete(file);
}

string[] directories = System.IO.Directory.GetDirectories(directoryName);
foreach(string directory in directories)
DeleteDirectory(directory);

System.IO.Directory.Delete(directoryName, true);
}
jlhdlj 2003-07-30
  • 打赏
  • 举报
回复
http://www.pcfans.com.cn/developer/code/story/0,2000081534,39102247,00.htm

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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