Wednesday, January 24, 2018

How to delete files from directory using c# MVC

In this article how to remove multiple files remove from 1 single directory using foreach loop in MVC using c#

string FilePath = HttpContext.Server.MapPath("~/YourMainDirectoryName/");
 
// First check if directory exists or not
 
if (!Directory.Exists(FilePath))
{
 string[] files = Directory.GetFiles(FilePath);
 
 if (files.Length > 0)
 {
  foreach (string filePaths in files)
                      System.IO.File.Delete(filePaths);
 }
}
 
at the end of loop if you want to delete main directory write below line 
 
Directory.Delete(FileSavePath);
 
again if you want to create same blank directory write below line 
 
Directory.CreateDirectory(FileSavePath);

No comments :

Post a Comment