I have code that calls the ReadXml method of the DataSet class and passes in a file name ReadXml(strFileName). Occasionally this throws a System.IO.IOException because the file is being used by another process.
If I change the code to use the ReadXml(stream) method and pass in a FileStream like this:
using(FileStream fs = new FileStream(this.filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ))
{
MyDS.ReadXxml(FileStream);
}
Will that prevent the IOException from occuring? What is going on under the hood when you simply pass in a file name?
The
DataSetclass will create aFileStreamobject for you if you pass in the filename as a string. The overloaded method that takes aStreamas a parameter allows you to pass in aStreamobject if you have one instead of a filename.The version that takes a string as a parameter will simply create a FileStream and pass that onto the version that takes a Stream as a parameter.
You should use the overload that suits the data you have. In this case the string.