The BinaryWriter you're creating is locking the file. The file stays locked as long as your program is running because you did not dispose of it.
To do that, you can either dispose of it manually after each use :
var bw = new BinaryWriter(File.Open("c:\\Tmp\\" + a.Name, FileMode.OpenOrCreate)); bw.Write(((FileAttachment)a).Content); bw.Close();
Use the Using statement which will close the stream automatically:
using (BinaryWriter bw = new BinaryWriter(File.Open("c:\\Tmp\\" + a.Name, FileMode.OpenOrCreate))) { bw.Write(((FileAttachment) a).Content); }
or even better, use File.WriteAllBytes which handles the writer creation, file opening and stream closing by itself in one concise line :
File.WriteAllBytes("c:\\Tmp\\" + a.Name, ((FileAttachment) a).Content);