Monday, 26 August 2013

the process cannot access the file as its being used by another process

the process cannot access the file as its being used by another process

have been struggling with this error for ages. Below is the code is question
for ( Int32 Counter = 0; Counter < x.Rows.Count; Counter++)
{
using (FileStream bitmapFile = new
FileStream(@"c:/someOlddir/file1.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{
using (Bitmap uploadedbitmap = new Bitmap(bitmapFile))
{
using (System.Drawing.Image uploadedbitmapResized =
ExtensionHelpers.Resize(uploadedbitmap, 800, 600,
RotateFlipType.RotateNoneFlipNone))
{
uploadedbitmapResized.Save(@"c:/someNewdir/file1.txt",System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
/*errror occurss on this line */
File.SetCreationTime(@"c:/someNewdir/file1.txt",someDateTimeVariable);
}
The problem I am having with this code is that It works just fine for the
first 30 or 40 images in the collection but when i get to 50 or 60 I get
an error saying that the file I am trying to set the date-time to is being
used by another process. but this error only occurs when I am iterating
through a large collection and at the 40 or 50th position in the iteration
how can this be that the first 30 images work just fine and then all of a
sudden a file is now lock by a process. I have everything in using
statements but this error still persists.
Do I Have to some how wait until the file is saved before accessing it but
this shouldn't be the case since i am saving the file first and then
trying access it. what is wrong with code?

No comments:

Post a Comment