The action can't be completed because the file is open in IIS Worker Process

Faneournard

New Member
I am uploading a image for a user & saving it on a specified location. The code is working fine in normal scenario i.e. user selects a image & save it, it works.Challenge occurs when user selects a image( probably wrong image due to mistake) & don't save it, but again selects a new image & then saves it. This scenario gives me the error :"The process cannot access the file because it is being used by another process."When I try to delete image from the location at the time of error, file can't be deleted with message:"The action can't be completed because the file is open in IIS Worker ProcessClose the file and try again."Code is like this:\[code\] try { if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); msf = new MemoryStream(); bytes=FileUpload1.FileBytes; msf.Write(bytes, 0, bytes.Length); using (FileStream stream = new FileStream(folder + "/" + filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { //converting any graphic file type to jpeg format Image img = Image.FromStream(msf); img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); msf.WriteTo(stream); IsuploadSuccess = true; img.Dispose(); } } catch { IsuploadSuccess = false; } finally { if (msf != null) { msf.Close(); msf.Dispose(); } }\[/code\]I have tried adding "FileAccess.ReadWrite" & "FileShare.ReadWrite" in file stream, but doesn't work either with all options in File stream.Please help...
 
Back
Top