Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir,

how to write code for delete all files inside folder when i am running asp.net website?


Note, Dynamically it run and delete all files.

please give me a solutions?

By mohan
Posted

You can use a loop:
C#
string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
foreach (string filePath in filePaths)
   {
   File.Delete(filePath);
   }


You can use Array.ForEach with an anonymous method:
C#
Array.ForEach(Directory.GetFiles(@"c:\MyDir\"), delegate(string path) { File.Delete(path); });

Or you could use
C#
Directory.Delete(path, true);
Directory.CreateDirectory(path);
to remove the folder, it's contents, and all subdirectories, then re-create the folder.
 
Share this answer
 
C#
public void DeleteAllTempFiles()

{

foreach (var f in System.IO.Directory.GetFiles(Server.MapPath("~/temp")))

System.IO.File.Delete(f);

}
 
Share this answer
 
v2
First of all to do such things you should have the right security and read/write permissions on that folder.

Then you can try this piece of code :

C#
DirectoryInfo objDir = new DirectoryInfo(sPath);

 //Check whether path exists
 if (objDir.Exists)
 {
     FileInfo[] objFile = objDir.GetFiles();
     for (int iIndex = 0; iIndex < objFile.Length; iIndex++)
     {
            File.Delete(objFile[0].DirectoryName + "\\" + objFile[0].Name);
     }
 }
 
Share this answer
 

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