Hello !

There is some code that generates a file. By clicking on the Button , this file is downloaded. I want to make it removed from the server.

I wrote this code here:

 public ActionResult DeleteAllFiles(string path) { DirectoryInfo directory = new DirectoryInfo(Server.MapPath(@"~/Content/")); var files = directory.GetFiles().ToList(); foreach (var deletedFile in files) { if (deletedFile.Extension == ".jpg") { try { if (deletedFile.Exists) { System.IO.File.Delete(deletedFile.FullName); } } catch (Exception e) { return View("Error" + e); } } return View(); } 

I do not quite understand where I need to use it. That is, that would not be such an error:

the file is busy by another process or the action cannot be performed because this file is open in Program_Name

UPD:

What are the ways to get the path to the files on the server? That is, how can I get the path to the generated file. Thank !

  • Do you need to delete one file or all ".jpg" files? - Igor
  • @Igor preferably the one that generated. I tried to delete absolutely all files. - kxko
  • "how can i get a way" ?? - Server.MapPath - Igor
  • @Igor that is, with the help of 'Server, MapPath' I get access to all the files on the server? That is, my algorithm will work for all .jpg files and clear the server of them? - kxko
  • one
    Apparently when you generate a jpg file, you do not release it, it is most likely occupied by the IIS process. - igosh

0