3

I have a tif files from a specific directory, I have a function to read the all the tif files and converted it to text, after the conversion I move it to a folder named "Completed" once it was successfully converted, and move it Failed folder once it was failed to convert. the Problem is the when I used the Move method of System.IO.File it was raised an error saying that

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

What is the problem, I closed the file , I dispose it , but still the error occurs?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user335160
  • 1,364
  • 6
  • 32
  • 67

4 Answers4

1

Be sure you close the file, as well as anything associated with it during the processing.

(since you chose not to show us code, we don't really know what got created during the processing.)

Look for things like MemoryStreams, MemoryMapped Files, Images backed by files, File handles stored in a container or IEnumerable, etc.

You have the right idea making sure you explicitly .Close() known references, and Dispose of objects, but something is still holding the file.

abelenky
  • 63,815
  • 23
  • 109
  • 159
1

I had a problem like this and the issue was that I wasn't able to delete the original file if I created it using the "FromFile" method of the Image class. Instead, I created it using the "FromStream" method (making sure to Close and Dispose of it at the end).

I modified my code to work like method #3 here: http://dotnetguts.blogspot.com/2009/07/process-cannot-access-file-because-it.html

Mikey
  • 2,837
  • 2
  • 18
  • 17
0

Make sure your conversion process has completely finished before moving. Sometimes it looks like a process is done when it really isn't.

thursdaysgeek
  • 7,696
  • 20
  • 78
  • 115
0

Make sure you close StreamReader, StreamWriter and any other processes that are operating the file before you perform a different operation with it. e.g. If you are reading with StreamReader and need to write to he file close StreamReader and then start StreamWriter.

Alternatively you could use Filestream and I have heard that in the .NET Framework 4.0 they have introduced asynchronous file operations.

liamzebedee
  • 14,010
  • 21
  • 72
  • 118