Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
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
Posted
Comments
Sergey Alexandrovich Kryukov 22-Jan-12 2:48am    
"Does not work for me" tells nothing about your problem.
--SA

1 solution

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:
C#
Directory.Delete(@"D:\Temp\DELME", true);
The folder was deleted.
Recreated the folder.
C#
Directory.Delete(@"D:\Temp\DELME.zip", true);
The command threw an exception, as DELME.zip is not a folder.
Checked both present.
C#
File.Delete(@"D:\Temp\DELME");
Threw an exception - unauthorised access (it is a folder, so yes it would fail).
C#
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?
 
Share this answer
 
Comments
Mohammad Mahdi Nejati 22-Jan-12 5:41am    
i know this codes. but the problem is that i want to import this addresses (d:\temp....) from a ListBox ! but an error happens! it says it can not convert string to object! let me show you what i wrote:
for(int i=0;i<listbox.lenght;i++){
file.delete(listbox.items[i]);
}
in the listbox i stored the path of the files that should be deleted. but i can not delete them with this code.
OriginalGriff 22-Jan-12 5:48am    
I suspect your error message is the other way round:
"Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)"
Try:
File.Delete((string) myListBox.Items[i]);
You also might want to consider using a foreach instead of the for:
foreach (object o in myListBox.Items)
{
File.Delete((string) o);
}
It is a bit cleaner to read if you aren't using the loop variable for anything else.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900