Delete all files

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

Is there a way to delete all files under a directory. Just the files
directly under that directory.
I know about Directory.Delete but this would delete the directory.

Thanks,
Miguel
 
Hello,

Is there a way to delete all files under a directory. Just the files
directly under that directory.
I know about Directory.Delete but this would delete the directory.

Thanks,
Miguel

I found a way:

// DeleteAll
public void DeleteAll(String path) {

// Delete files
String[] files = Directory.GetFiles(path);
foreach (String f in files) File.Delete(f);

} // DeleteAll

I also tried:

Array.ForEach(Directory.GetFiles(path), delegate(string path)
{ File.Delete(path); });

But it does not compile.

Thanks,
Miguel
 
However if you'd prefer to do this in two lines of code, delete the
directory and then recreate it... :-)

If I could make the following work I would do it in one line :-P
Array.ForEach(Directory.GetFiles(path), delegate(string path)
{ File.Delete(path); });
 
This won't do what you want if the folder contains subfolders...

Yes but in that case deleting the directory will also make difficult
to recreate directory structure.
 
What about:
string[] filenames = Directory.GetFiles(path, "*",
SearchOption.AllDirectories);
Then loop through each filename and delete it. In this case I have the
SearchOption for all directory or just for the top directory.
 
What about:
string[] filenames = Directory.GetFiles(path, "*",
SearchOption.AllDirectories);
Then loop through each filename and delete it. In this case I have the
SearchOption for all directory or just for the top directory.

foreach(string fileName in Directory.GetFiles(targetDirectory)
{
    File.Delete(fileName);

}

Yes I did something like that.
The paths are correct but the files are not deleted.
And I don't get any error which is strange.

// Delete
public void Delete(String path, String pattern, SearchOption
option) {
String[] files = Directory.GetFiles(path, pattern, option);
foreach (String f in files)
File.Delete(new Uri(f).LocalPath);
} // Delete

And I also tried simply with:
File.Delete(f);

And then I use it as follows:

fileService.Delete(controller.Server.MapPath("~/Data"), "*",
SearchOption.AllDirectories);

I also tried with controller.Server.MapPath("~/Data/") but the files
are not deleted either.

The paths I get are something as follows:
"C:\\Users\\Miguel\\Projects\\Std\\Solution\\Std\\Data\\Assets.xml"

And it is correct.

Any idea why the files are not being deleted?

Thanks,
Miguel
 
I also tried with controller.Server.MapPath("~/Data/") but the files
are not deleted either.

The paths I get are something as follows:
"C:\\Users\\Miguel\\Projects\\Std\\Solution\\Std\\Data\\Assets.xml"

And it is correct.

Any idea why the files are not being deleted?

In addition to Mark's comments, what OS are you running? If it is
Vista, you may be running into file system virtualization.

http://thelazyadmin.com/blogs/thelazyadmin/archive/2007/04/26/file-system-virtualization.aspx

Chris
 
Back
Top