Link to home
Start Free TrialLog in
Avatar of brdrok
brdrok

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of kingtam2000
kingtam2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brdrok
brdrok

ASKER

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 =)

Glad to help.