1

I'm saving a canvas image to the server using FileStream. It works fine the first time, but if the user visits the page again during the same session (without closing the browser) it throws the error "The process cannot access the file 'C:\inetpub\wwwroot\CarmelFinancialWeb\Contracts\MyUniqueImageFileName.png' because it is being used by another process." I tried to use close and dispose on the FileStream, but this is still occurring

Process: - User goes to AssignContract page, signs in the canvas, clicks the save button. - Image is saved to the file - User hits the save button again (even after waiting a few minutes) - Error message saying file is used by another process.

[AuthorizeAdmin]
    [HttpPost]
    public ActionResult AssignContract(string ID, ModelContract tempCurrentContract, string imageData)
    {
        try
        {
            if (imageData != null)
            {
                string fileName = "MyUniqueImageFileName.png";
                string fileNameWitPath = Path.Combine(Server.MapPath("~/Contracts"), fileName);

                using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        byte[] data = Convert.FromBase64String(imageData);
                        bw.Write(data);
                        bw.Close();
                        bw.Dispose();
                    }
                    fs.Close();
                    fs.Dispose();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
...

Update

Haven't had any feedback on this. Anyone have a suggestion on how to prevent the file from being locked or how to unlock the file? Thank you.

boilers222
  • 1,901
  • 7
  • 33
  • 71
  • 1
    You aren't locking the file here. Something else is happening. If I had to guess, I'd say a virus scanner. Best to watch that file after it gets created and see what process is locking it. –  Mar 26 '15 at 16:43
  • Also you don't need to call fs.Dispose the using block will do that for you. Maybe a call to fs.Flush might help. (I'm not that good with FileStreams!!) – matt_lethargic Mar 26 '15 at 16:56
  • Thanks for the comments. Interesting that a virus scanner could lock it. How would I tell what is locking the file? I have another place that puts the signature on a pdf. Could it be causing the locking? If so, how do i unlock it? System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/Contracts/MyUniqueImageFileName.png")); – boilers222 Mar 26 '15 at 18:20
  • Using a program called Process Explorer (http://superuser.com/questions/399659/how-can-i-identify-what-application-is-using-a-given-file), I can see that iisexpress.exe is locking the file. So something in my code is locking it. Whether it's the creating the file or reading from the file, I don't know. How do I find out and how do I fix it? – boilers222 Mar 26 '15 at 18:30
  • I don't think it matters here but I think you're closing the handle twice. The handle from fs is passed to bw when you call new. You should set fs = null after you dispose() bw. Then wrap the fs.close(); fs.dispose() in if (fs != null) so the handle is closed in case an exception is thrown. – Skye MacMaster Apr 06 '15 at 20:32

1 Answers1

0

The problem seems to be the file is locking each time I access the image to output it to another process. So for every instance where I access it:

System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/Contracts/MyUniqueImageFileName.png"));

I had to dispose of the image:

image.Dispose();

I did notice if I waited a few minutes and tried to save a new image, it was not locked. So maybe the Dispose function happens automatically after a period of time? If anyone knows that or has a way to unlock all files before writing a new one, I'd be interested in how that's done.

boilers222
  • 1,901
  • 7
  • 33
  • 71