1

I am trying to delete a folder but i can manage to get it right?

The folder i am trying to delete is called ExtractedFiles and it is inside a folder called FormValue.

I can delete a spreadsheet in the same FormValue folder but cant delete the folder.

I think the problem might be that i don't have the right file extension for the folder

This works:

if (File.Exists(tempFolderPathAlt + saveas + ".xls"))
            {
                File.Delete(tempFolderPathAlt + saveas + ".xls");
            }

This does not work:

 if (File.Exists(tempFolderPathAlt + "ExtractedFiles"))
            {
                File.Delete(tempFolderPathAlt + "ExtractedFiles");
            }

Could someone please tell me the file extension of a folder or how to delete one?

Pomster
  • 14,567
  • 55
  • 128
  • 204

5 Answers5

5

If you want to delete a folder, you should use Directory.Delete instead of File.Delete:

String path = Path.Combine(tempFolderPathAlt, "ExtractedFiles");
bool directoryExists = Directory.Exists(path);
if(directoryExists)
    Directory.Delete(path, true); // deletes sub-directories
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Try using the Directory.Delete method.

Joe
  • 122,218
  • 32
  • 205
  • 338
0

you want

Directory.Delete

Since your deleting a folder and not a file

saj
  • 4,626
  • 2
  • 26
  • 25
0

For deleting dirrectories, you need to use the method

Directory.Delete(string path,
    bool recursive);

Refer official docs here: http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0

Check this out if you are getting IOException deleting directories :

Cannot delete directory with Directory.Delete(path, true)

Community
  • 1
  • 1
arpad
  • 541
  • 5
  • 10