The Problem : I was using
the StreamReader and StreamWriter C# classes in many Script tasks of my SSIS
package. I was trying to read an XML file, perform some operations on it, copy
the read file to another folder etc etc. After a while I started getting this weird
error that "The operation cannot perform… the resource is already in
use". The error was coming from the XML file which I was trying to copy
from one folder to the other. I checked - my file was not physically open
anywhere. What could be using it?
The Solution : The file
was not physically open anywhere but it was programmatically open! I had forgotten to do a streamReader.Close();
after my initial read. As a result, the file was still held by the streamReader
object, not releasing it to the FileSystem Task which was trying to copy it to
another folder.
Here is my code
snippet-
StreamReader
streamReader = new StreamReader(filePath);
String streamText =
streamReader.ReadToEnd();
this.Dts.Variables["XMLFileContent"].Value
= streamText;
streamReader.Close();
Dts.TaskResult =
(int)ScriptResults.Success;
Where "filePath" is
the absolute file path of my file in the folder
( including the file name).