Solved

Error: The process cannot access the file because it is being used by another process

Posted on 2007-03-28
4
4,102 Views
Last Modified: 2008-01-09
Hello,

I am getting a:

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

error message.  How do I handle this problem?  Below is my code:

public static void MoveFile(string fileName, string destinationFolder)
{
   try
     {
        string source = Path.Combine(Constants.CompleteStagingFolderPath, fileName);
        string destination = Path.Combine(destinationFolder, fileName);

        File.Copy(source, destination, false);
        }
        catch (System.IO.IOException ioEx)
        {
           System.Guid guid = System.Guid.NewGuid();

           if(ioEx.Message.EndsWith("already exists."))
           {
              string source = Path.Combine(Constants.CompleteStagingFolderPath, fileName);
              string newFileName = fileName.Insert(fileName.Length - 4, "_v" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute + DateTime.Now.Second);
                   
              File.Move(source, Path.Combine(destinationFolder, newFileName));  <===Generating error
              }
         }
         catch (Exception ex)
         {
             //do something else
         }
}


thanks
0
Comment
Question by:brdrok
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
kingtam2000 earned 250 total points
Comment Utility
It could be that the file you are trying to move has been locked by another process or another file has a handle on it.  Thus, when you move it, it throws the exception.  If you want to see which process is currently accessing it, then there is a tool, called Process Monitor, which will tell you which program is accessing it.  You can get it from http://www.microsoft.com/technet/sysinternals/utilities/ProcessExplorer.mspx
As a side note, was it intentional that it only moves the file if the destination file already exists, as the function at the moment copies the file, thus not causing any error, if the destination file does not exist, and only moves if the destination file does exist?
0
 
LVL 1

Assisted Solution

by:juniorDev
juniorDev earned 250 total points
Comment Utility
The problem you are having may be that the file is still open or locked from the first instance of:
File.Copy(source, destination, false);
in the "Try" block.  You might need to close or unlock the "source" file in the "Try" block before you attempt
File.Move(source, Path.Combine(destinationFolder, newFileName));

Since the varaible's scope is within the "Try" block, you should probably declare the "source" variable outside the Try block and then set the instance of it in the Try block to give it greater scope.  You may need to use a boolean variable in the catch block to determine if the specific exception you are trying to catch is thrown.

public static void MoveFile(string fileName, string destinationFolder)
{
   string source;
   string destination;
   bool ioEx = false;
   try
     {
        source = Path.Combine(Constants.CompleteStagingFolderPath, fileName);
        destination = Path.Combine(destinationFolder, fileName);
        File.Copy(source, destination, false);
       }
        catch (System.IO.IOException ioEx)
        {
           System.Guid guid = System.Guid.NewGuid();

           if(ioEx.Message.EndsWith("already exists."))
           {
             ioEx = true;
              }
         }
         catch (Exception ex)
         {
             //do something else
         }
}
Use a "Finally" block to release the file.  Check the value of the boolean variable and, if it is true, call another funciton to execute the File.Move process.
0
 
LVL 7

Author Comment

by:brdrok
Comment Utility
Thanks for the pointers.  Turned out that my FileShare or FileMode, I can't remeber which one, has been set to None, thus, nobody was able to access it.  Dummy me =)

0
 
LVL 1

Expert Comment

by:juniorDev
Comment Utility
Glad to help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now