Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-28
4
Medium Priority
?
4,118 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 1000 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 1000 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

Technology Partners: 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 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