Jason
Jason

Reputation: 233

Trouble deleting a folder full of files

This should be very simple but I'm not sure what's wrong. I'm trying to delete all the files in a folder using File.Delete.

This is what I have so far:

DirectoryInfo ImageFolder = new DirectoryInfo(Program.FolderPath + @"\Images");
foreach (var File in ImageFolder.GetFiles())
{
    File.Delete(File.FullName);
}

Then the ".Delete" becomes underlined and says no overload for method delete takes 1 argument.

Any help is appreciated.

Upvotes: 1

Views: 77

Answers (3)

user6522773
user6522773

Reputation:

To delete folder full of file use:

Directory.Delete(string directoryName, bool recursive);

https://msdn.microsoft.com/en-us/library/fxeahc5f(v=vs.110).aspx
Or from your code above, use:

DirectoryInfo ImageFolder = new DirectoryInfo(Program.FolderPath + @"\Images");
foreach (var fileInfo in ImageFolder.GetFiles())
{
    fileInfo.Delete(); //this is FileInfo.Delete
    // or
    // File.Delete(fileInfo.FullName);
    // dont use reserve "File" as your variable name
}

Remember, you are calling FileInfo, not File

Upvotes: 1

Jeremy Thompson
Jeremy Thompson

Reputation: 65544

What you're seeing is called Namespace Ambiguity.

In your own code or a reference DLL you probably have a method called Delete in a Class called File that doesn't support a single string parameter.

To fix the problem fully qualify File.Delete with System.IO, eg:

System.IO.File.Delete

Upvotes: 1

wake-0
wake-0

Reputation: 3968

You have to change the naming of the variable:

DirectoryInfo ImageFolder = new DirectoryInfo(Program.FolderPath + @"\Images");
foreach (var file in ImageFolder.GetFiles())
{
    File.Delete(file.FullName);
}

Upvotes: 0

Related Questions