在C#中删除目录 [英] deleting directorys in c#

查看:93
本文介绍了在C#中删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试编写一个程序,该程序搜索文件夹及其所有子文件夹,以查找该文件夹中的zip或Rar文件,并且旁边有一个名为zip文件的文件夹.找到它们后,它将文件的路径发送到列表框中.但是我在导入这些地址和删除目录或文件时有问题(根据用户选择).我知道有这样的代码可以执行此操作,但是它对我不起作用.

 System.IO.Directory.Delete( @" 解决方案

我不是很肯定,但您的问题确实是,但我试过了:
在我的Temp目录中创建一个名为"DELME"的文件夹
在我的Temp中创建一个zip文件,直接称为"DELME.ZIP"

以下代码有效:

 Directory.Delete( @"  true ); 

该文件夹已删除.
重新创建文件夹.

 Directory.Delete( @"  true ); 

该命令引发异常,因为DELME.zip不是文件夹.
检查了两个都存在.

 File.Delete( @" ) ; 

引发异常-未经授权的访问(它是一个文件夹,所以会失败).

 File.Delete( @" ); 

该zip文件已删除.

所有这些对我来说都很好:您在做什么可能会有所不同?


hi
i am trying to write a program that search a folder and all its subfolders for finding zip or Rar files that are in the folder and there is a folder named exactly like the zip files beside it. after finding them it send the path of the files into a listbox. but i have problems with importing these addresses and deleting the directories or the files(based on the user choice). i know that there is a code like this for doing this but it doesn''t works for me.

System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest", true);



please answer just simply i am not that much pro in programming. and i am working with visual c# so please don''t use command line codes.thank very much

解决方案

I''m not positive whet your problem is exactly, but I tried this:
Create a folder in my Temp directory called "DELME"
Create a zip file in my Temp directly called "DELME.ZIP"

The following code worked:

Directory.Delete(@"D:\Temp\DELME", true);

The folder was deleted.
Recreated the folder.

Directory.Delete(@"D:\Temp\DELME.zip", true);

The command threw an exception, as DELME.zip is not a folder.
Checked both present.

File.Delete(@"D:\Temp\DELME");

Threw an exception - unauthorised access (it is a folder, so yes it would fail).

File.Delete(@"D:\Temp\DELME.zip");

The zip file was deleted.

All of that looks fine to me: what are you doing that could be different?


这篇关于在C#中删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆