The process cannot access the file 'xxx' because it is being used by another process

Hello everyone,

I did a search and found another post that was similar but not quite the same as my problem.  I have an SSIS package I'm trying to build that will look in a source directory for a file, check to see if it's locked, and if it isn't, moves it to another directory.  Here's my issue:

I have a C# script task that attempts to open the source file to check if it's locked...

        public void Main()
        {
            // Declare local variables
            String strFileName = Convert.ToString(Dts.Variables["FileName"].Value);
            FileStream fsMyFile;

            try
            {
                // Attempt to open file to check if it's locked by another process.  
                // If able to open, it is unlocked and can be passed to the next task
                fsMyFile = File.Open(strFileName, FileMode.Open, FileAccess.ReadWrite,FileShare.None);
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch(Exception e)
            {
                // Attempt to open file failed, leave file for next job run
                Dts.TaskResult = (int)ScriptResults.Failure;
                throw e;
            }
           
        }

The problem in the next task, after successfully determining that the file is able to be opened, I use a file system task to move the file to the target directory but run into the error message: "The process cannot access the file 'xxxx' because it is being used by another process.  I've tried several things already including having the thread sleep for a few seconds before the file system task but nothing works.  Has anyone else run into a problem like this?  I'm trying to avoid delay tactics as I'm attempting to move multiple files within a pre-determined length of time.

Thanks in Advance,
Glen
jisoo411Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jisoo411Author Commented:
I finally figured it out.  I added fsMyFile.Close() to the try statement right before the Dts.TaskResult line which releases any resources devoted to that open file.  I guess garbase collection needs to be explicitly demanded in this particular case.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Megan BrooksSQL Server ConsultantCommented:
You might try closing the file, perhaps in a finally{} block as part of the try/catch.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.