Solved

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

Posted on 2007-03-28
4
4,103 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
ID: 18810581
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
ID: 18813574
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
ID: 18826522
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
ID: 18826555
Glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

20 Experts available now in Live!

Get 1:1 Help Now