-1

Using C# and .net 4.8 I have an API method where users upload a file.

I save the file to a temp directory, do some processing, then save it to the final destination directory.

At the end of the process I want to delete the file from the temp directory, but I get an exception:

The process cannot access the file because it is being used by another process.

on this line:

File.Delete(originalTmpPath);

To narrow down the problem I have remmed out all code that does additional processing so now my code does only 3 things:

  1. Save the file to the temp directory.
  2. Save the file “In the temp directory” to another directory.
  3. Delete the first file

Here’ the code in the API:

HttpResponseMessage result = null;
var httpRequest = HttpContext.Current.Request;

var file = httpRequest.Files[0];
// Save image to temp folder
file.SaveAs(originalTmpPath);

// Save image to final folder
Bitmap bmp = new Bitmap(System.Drawing.Image.FromFile(originalTmpPath));
bmp.Save(originalPath);

//Exception here:
File.Delete(originalTmpPath);

Any idea how I can stop the process/unlock the file and delete it?

Thank you

T.S.
  • 18,195
  • 11
  • 58
  • 78
spacedog
  • 446
  • 3
  • 13
  • 2
    Right here `System.Drawing.Image.FromFile(originalTmpPath)` you **probably** not releasing resources. Use a variable and call `dispose`. If this works, use `using (var img = ...)` in the final version of the code – T.S. Jul 21 '21 at 23:59
  • You need to `Dispose()` of `bmp` before you can delete it. Possibly, declare the Bitmap object with a `using` statement (`Save()` inside the `using` block). `Clone()` the mage if you still need the bitmap after. Whatever graphic object you create, you need to dispose of it anyway. – Jimi Jul 22 '21 at 00:48
  • Interesting. I thought SO questions couldn't have identical titles.. – Caius Jard Jul 22 '21 at 05:29

1 Answers1

2

use InputStream in file if you are not manipulating the file.

Bitmap bmp = new Bitmap(file.InputStream);
bmp.Save(originalPath);

then

File.Delete(originalTmpPath);

Of course, saving the file to the server is useless.

or use

using(Bitmap bmp = new Bitmap(originalTmpPath))
{
    bmp.Save(originalPath);
}
File.Delete(originalTmpPath);
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
  • 1
    the whole point of using (no pun intended) `using` is not to call `bmp.Dispose();` explicitely – T.S. Jul 22 '21 at 02:23
  • with bmp.Dispose(); the code runs without error. But I deleted it. – Meysam Asadi Jul 22 '21 at 02:29
  • *"code runs without error"*. It is better be. Because if you get error `Dispose` is not properly implemented. But then again `using` is `try/catch/finally` and `Dispose` is called in that `finally` if the object implements `IDisposable`. https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/using-objects – T.S. Jul 22 '21 at 13:32
  • with "using" the sample is executed in memory and at the end of the operation or error occurs, the memory is freed and the sample is destroyed. The Dispose () method is called when an instance exists at the end of an operation or error. – Meysam Asadi Jul 22 '21 at 15:08