.net 同时删除文件和文件夹

lit0302 2009-05-08 04:41:03
我单独文件或则单独删夹都没错,但是如果同时文件和文件夹就出错了,为什么啊 。还有谁有更好的方法判断是文件夹还是文件?
代码:
protected bool Deletefile(string strfile)
{
string[] fs = filename.Split(new char[] { ',' }); //获得选中的文件名称
string path = "";
for (int i = 1; i < fs.Length; i++)
{
path = Request.Cookies["Info"].Values["path"].ToString() + "\\" + fs[i];
if (fs[i].LastIndexOf(".") != -1) //判断是文件还是文件夹
{
FileInfo file = new FileInfo(path);
if (!file.Exists) return false;
file.Delete();
}
else
{
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
dir.Delete();
}
}

}
return true;
}
...全文
312 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjp_auhtm 2009-05-09
  • 打赏
  • 举报
回复
1、判断路径为文件或文件夹的方法


/// <summary>
/// 判断输入路径为文件还是文件夹
/// </summary>
/// <param name="fnPath">输入的路径</param>
/// <returns>1:文件、2:文件夹、3:不存在</returns>
private int IsFile(string fnPath)
{
if (File.Exists(fnPath))
{
return 1;
}
else
{
if (Directory.Exists(fnPath))
{
return 2;
}
}
return 3;
}


2、这段代码“DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
dir.Delete(); ”中
dir.Delete()应该为“dir.Delete(true)

3、for循环中应作出fs[i]是否为空字符的判断
4、在for循环中,若路径不存在,不应是return,应该是continue
saisky 2009-05-09
  • 打赏
  • 举报
回复
先删除文件 再删除文件夹
ConanKid 2009-05-09
  • 打赏
  • 举报
回复
可以先删除文件再删除文件夹,可能会有文件夹里还有文件夹,也可能是好几层,其实没关系,可以递归调用,我这里有拷贝文件夹下所有文件和文件夹的代码,其实思路一样,你只要把拷贝操作换成删除操作即可,供参考:

Public Sub CopyDerictory(ByVal DirectorySrc As DirectoryInfo, ByVal DirectoryDes As DirectoryInfo)
Dim strDirectoryDesPath As String = DirectoryDes.FullName & "\" & DirectorySrc.Name

If Not Directory.Exists(strDirectoryDesPath) Then
Directory.CreateDirectory(strDirectoryDesPath)
End If

Dim f, fs() As FileInfo

fs = DirectorySrc.GetFiles()

For Each f In fs
File.Copy(f.FullName, strDirectoryDesPath & "\" & f.Name, True)
Next

Dim DirSrc, Dirs() As DirectoryInfo


Dirs = DirectorySrc.GetDirectories()

'递归调用自身
For Each DirSrc In Dirs
Dim DirDes As New DirectoryInfo(strDirectoryDesPath)
CopyDerictory(DirSrc, DirDes)
Next
End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CopyDerictory(New DirectoryInfo("C:\Documents and Settings\username\Favorites"), New DirectoryInfo("g:\temp"))
End Sub

daihua_1113 2009-05-09
  • 打赏
  • 举报
回复
学习
聖少俊 2009-05-08
  • 打赏
  • 举报
回复
学习
cj7421 2009-05-08
  • 打赏
  • 举报
回复
文件夹内有文件,只要删除就出错撒!只能删除空文件夹!
semify 2009-05-08
  • 打赏
  • 举报
回复
如果是夹的话要先删除夹内的文件和子文件夹,这个要用递归。

void delete(string path)
{
if(File.Exists(path))
File.Delete(path);
else
delDir(new DirectoryInfo(path));
}

void delDir(DirectoryInfo dir)
{
foreach (FileInfo f in dir.GetFiles())
f.Delete();
foreach (DirectoryInfo d in dir.GetDirectories())
delDir(d);
dir.Delete();
}
wuyq11 2009-05-08
  • 打赏
  • 举报
回复
private void deletefile(System.IO.DirectoryInfo path)
{
foreach(System.IO.DirectoryInfo d in path.GetDirectories())
{
deletefile(d);
}
foreach(System.IO.FileInfo f in path.GetFiles())
{
f.Delete();
}
}
ztenv 2009-05-08
  • 打赏
  • 举报
回复
DirectoryInfo d;
d.Delete(true)

110,538

社区成员

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

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

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