Solved

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

Posted on 2007-03-28
4
4,108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

710 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