1
votes
private void MoveFile(string destination, string imagePath)
{
    try
    {
        string source = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["TempImagePath"]);

        string[] resizedImage = imagePath.Split('.');
        //string compressedImagePath = resizedImage[0] + "_compress." + resizedImage[1];
        string thumbnail150Name = resizedImage[0] + "_thumbnail." + resizedImage[1];
        string mediumPhoto300Name = resizedImage[0] + "_mediumPhoto." + resizedImage[1];
        string largePhotoName = resizedImage[0] + "_largePhoto." + resizedImage[1];

        //Move thumbnail
        if (System.IO.File.Exists(source + "\\" + thumbnail150Name))
        {
            if (!System.IO.Directory.Exists(destination))
            {
                DirectoryInfo di = System.IO.Directory.CreateDirectory(destination);
            }
            System.IO.File.Move(source + "\\" + thumbnail150Name, destination + "\\" + thumbnail150Name);
        }

        //Move medium sized photo
        if (System.IO.File.Exists(source + "\\" + mediumPhoto300Name))
        {
            if (!System.IO.Directory.Exists(destination))
            {
                DirectoryInfo di = System.IO.Directory.CreateDirectory(destination);
            }
            System.IO.File.Move(source + "\\" + mediumPhoto300Name, destination + "\\" + mediumPhoto300Name);
        }

        //Move large photo
        if (System.IO.File.Exists(source + "\\" + largePhotoName))
        {
            if (!System.IO.Directory.Exists(destination))
            {
                DirectoryInfo di = System.IO.Directory.CreateDirectory(destination);
            }
            System.IO.File.Move(source + "\\" + largePhotoName, destination + "\\" + largePhotoName);
        }

        //Move original
        if (System.IO.File.Exists(source + "\\" + imagePath))
        {
            if (!System.IO.Directory.Exists(destination))
            {
                DirectoryInfo di = System.IO.Directory.CreateDirectory(destination);
            }
            System.IO.File.Move(source + "\\" + imagePath, destination + "\\" + imagePath);

        }
    }
    catch (Exception ex)
    {
        BgLogger.FileLogger.LogError("In private method : MoveFile(string, string), On server : " + Dns.GetHostName() + " At : " + DateTime.Now, ex).Wait();
    }
}

I am using this code while moving an image file. But I am getting the following exception

System.IO.IOException: The process cannot access the file because it is being used by another process.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalMove(String sourceFileName, String destFileName, Boolean checkHost) at BgPortal.Controllers.DriverController.MoveFile(String destination, String imagePath) https://u3307993.ct.sendgrid.net/wf/open?upn=mzyvlCvUM5odb-2FP3IS9fxavC9Nq-2BVU-2BlgxLxmreq6Qi5SziY4JAfC5R720TrBfTXj7GdvSOgEG2Qeq-2BGKm-2FwWmPOrbAem6UFnvcBKca-2FzIM3XkXg0dHdke9rhjcAKDqtd0MJQCnmyIN-2Fi-2FXbgygtnXFNxuEuwts4hybPsnVR72PsfW4L6YZ32pnlEuwGMF-2Fcg0S8f8Y7UOBHwDMzh1BgJnhqO9i5dgS9LRZytY4n6TNCt37JAtdi5EOj8OxBqhan

This exception occurs very randomly.

Can anyone help me with this?

1

1 Answers

0
votes

The exception is thrown because the file you are trying to move is open by your program or another process. Make sure that you are always closing the file after you finish processing it. Particularly, make sure that you always call Close or Dispose on all FileStream objects when they are no longer needed.

If the file is open by another process (for example by another user) then you can wait for some time and retry moving the file.