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

I saw many such problems that were resolved, and the problem was mainly due to the fact that the threads were not disposed of properly.

My problem is a little different, follow the code snippet here

foreach (Images item in ListOfImages) { newPath = Path.Combine(newPath, item.ImageName + item.ImageExtension); File.Create(newPath); File.WriteAllBytes(newPath, item.File); } 

Where Images is a custom structure and item.File is raw data, byte [].

My problem is that an exception is WriteAllBytes in the line where WriteAllBytes is called. The message says:

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

Again I donโ€™t know how somehow close have to go through the process.

+8
source share
7 answers

Since File.Create returns a stream, I would have disposed it correctly:

 using(var stream = File.Create(newPath)){} File.WriteAllBytes(newPath, item.File); 

or you can use the stream to write directly to a file:

 using (FileStream fs = File.Create(newPath)) { fs.Write(item.File, 0, item.File.Length); } 

or perhaps the easiest, use File.WriteAllBytes :

 File.WriteAllBytes(newPath, item.File); 

Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.

+19
source

You state that your problem has nothing to do with thread deletion, but check this MSDN article:

http://msdn.microsoft.com/en-us/library/d62kzs03.aspx

What returns File.Create ? FileStream !!!!

And finally, why do you use File.Create if File.WriteAllBytes creates a file if that doesn't exist ?;)

Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.

Also check this out on MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx

+4
source

The create method opens the file for writing and returns the FileStream object for work. Just because you are not referring to it does not mean that it does not need to be returned.

 foreach (Images item in ListOfImages) { newPath = Path.Combine(newPath, item.ImageName + item.ImageExtension); FileStream f = File.Create(newPath); f.Write(item.File, 0, item.File.Length); } 
+1
source
 using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 

Your log may be locked, so try with FileShare.ReadWrite.

+1
source

File.WriteAllBytes creates a file if necessary. You can use:

 foreach (Images item in ListOfImages) { newPath = Path.Combine(newPath, item.ImageName + item.ImageExtension); File.WriteAllBytes(newPath, item.File); } 

And are you combining the path correctly?

0
source

This is the most specific way to accomplish what you are trying to do:

 foreach (Images item in ListOfImages) { using (System.IO.FileStream output = new System.IO.FileStream(Path.Combine(newPath, item.ImageName + item.ImageExtension), System.IO.FileMode.Create, System.IO.FileAccess.Write)) { output.Write(item.File, 0, item.File.Length); output.Flush(); output.Close(); } } 

You also need to correct your logic to create the path that I made in my example above. You have contacted newPath again and again.

0
source

Force clean the garbage collector.

 GC.Collect(); 
-2
source

All Articles