Solved

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

Posted on 2007-03-28
4
4,104 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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