Every time I save a file and delete it right away using the function below, I keep getting this error message: "System.IO.IOException: The process cannot access the file because it is being used by another process".
Waiting for a couple of minutes or closing visual studio seems to only unlock the files that you uploaded previously.
public static bool DeleteFiles(List<String> paths) { // Returns true on success try { foreach (var path in paths) { if (File.Exists(HostingEnvironment.MapPath("~") + path)) File.Delete(HostingEnvironment.MapPath("~") + path); } } catch (Exception ex) { return false; } return true; } I think that the way I'm saving the files may cause them to be locked. This is the code for saving the file:
if (FileUploadCtrl.HasFile) { filePath = Server.MapPath("~") + "/Files/" + FileUploadCtrl.FileName; FileUploadCtrl.SaveAs(filePath) } When looking for an answer I've seen someone say that you need to close the streamReader but from what I understand the SaveAs method closes and disposes automatically so I really have no idea whats causing this
FileOptions.DeleteOnCloseas you can see here: stackoverflow.com/questions/3240968/…using?