I know File.Open()
internally calls new FileStream()
. So why does code utilizing File.Open()
throw an IOException
with the error message:
The process cannot access the file because it is being used by another process
Code with using FileStream()
does not cause this error.
for (int i = 0; i < 20000; i++)
{
XmlSerializer x1 = new XmlSerializer(typeof(Tasks));
using (FileStream fileStream = File.Open(
Settings.Default.DownloadJobFileName,
FileMode.Open,
FileAccess.Read))
{
tasks = ((Tasks)xmlSerializer.Deserialize(fileStream));
}
XmlSerializer x2 = new XmlSerializer(typeof(Tasks));
using (FileStream fileStream = File.Open(
Settings.Default.DownloadJobFileName,
FileMode.Create,
FileAccess.Write))
{
xmlSerializer.Serialize(fileStream, tasks);
}
}
I'm using Windows 8.1 and .Net 4.0 client profile, seems it's getting worse on Windows Embedded Standard 2009