views:

124

answers:

3

I'm having an issue opening files that have recently been closed by the .Net framework. Basically, what happens is the following:

  • Read in an XML file using DataSet.ReadXml()
  • Make some changes to the data
  • Write out the XML file using DataSet.WriteXml()
  • Copy the XML file to a new location using File.Copy
  • FTP the file using a custom control

This sequence can intermittently fail either after the WriteXML or the File.Copy with a file in use exception.
I'm guessing it could be the Windows write cache not flushing right away. Can anyone confirm that this could be causing my issue? Any solutions to suggest?

Thanks,
Dan

A: 

Note: If your using FileStream remember to close it.

JeremySpouken
A: 

Not sure how you are opening the file.

I was having the same problem after closing a text file.

This is what I found that worked:

if (null != sReader) ((IDisposable)sReader).Dispose();

where sreader is

StreamReader sReader

Joe Pitz
Even better, if you can -- use the 'using' statement so you don't have to think about disposing it.
Nazadus
+1  A: 

Could this possibly be caused by an over eager anti virus program? They may place a lock on it while they inspect the file

Development 4.0