[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2010-04-02
2
Medium Priority
?
1,724 Views
Last Modified: 2013-11-10
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
0
Comment
Question by:jisoo411
2 Comments
 

Accepted Solution

by:
jisoo411 earned 0 total points
ID: 29493414
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
 
LVL 16

Assisted Solution

by:Megan Brooks
Megan Brooks earned 500 total points
ID: 29494144
You might try closing the file, perhaps in a finally{} block as part of the try/catch.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question