asp. net programming method for deleting folders and files under folders

  • 2021-07-07 07:17:22
  • OfStack

This article describes the example of asp. net programming to delete folders and folders under the file method. Share it for your reference, as follows:


// Get Folder 
string path = Server.MapPath("Image");
// Get all the pictures in the folder 
if (Directory.GetFileSystemEntries(path).Length > 0) 
{
  // Traverse all files in the folder 
  foreach (string file in Directory.GetFiles(path))
  {
   // File already exists 
   if (File.Exists(file)) 
   {
    FileInfo fi = new FileInfo(file);
    // Determine whether the current file attribute is read-only 
    if (fi.Attributes.ToString().IndexOf("ReadyOnly") >= 0) 
    {
     fi.Attributes = FileAttributes.Normal;
    }
    // Delete a file 
    File.Delete(file);
   }
  }
  // Delete Folder 
  Directory.Delete(path);
}

I hope this article is helpful to everyone's asp. net programming.


Related articles: