8

I have a code to delete folder and all files in it. I need to delete only files inside folder and not folder itself folder "1" for example must remain)... How can this be done using this code?

public class Deletefolder
    {
        public static void Main()
        {

           var dir = new DirectoryInfo(@"C:\d\wid\1");
            dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;

                dir.Delete(true);

            }

        }
greg-449
  • 109,219
  • 232
  • 102
  • 145
Raven111
  • 81
  • 1
  • 1
  • 3
  • If you're not going to delete the folder, I'd rename your class to "EmptyFolder" or something similar. – Adam V Oct 10 '17 at 13:21
  • Possible duplicate of [How to delete all files and folders in a directory?](https://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory) – Adam V Oct 10 '17 at 13:21

1 Answers1

12

You could use following code:

System.IO.DirectoryInfo di = new DirectoryInfo("YourPath");

foreach (FileInfo file in di.GetFiles())
{
    file.Delete(); 
}

Directly 'stolen' from this answer: https://stackoverflow.com/a/1288747/1661209

I think this question is almost an exact duplicate of that one.

Community
  • 1
  • 1
Max
  • 12,622
  • 16
  • 73
  • 101