Link to home
Start Free TrialLog in
Avatar of jisoo411
jisoo411

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of jisoo411
jisoo411

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial