移动图像文件时发生IOException

时间:2016-08-26 09:00:52

标签: c# .net

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();
    }
}

我在移动图像文件时使用此代码。但我得到以下异常

  

System.IO.IOException:进程无法访问该文件,因为它   正在被另一个进程使用。

     

at System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)   在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

此异常非常随机发生。

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

抛出异常,因为您尝试移动的文件是由您的程序或其他进程打开的。完成处理后,请确保始终关闭文件。特别是,请确保在不再需要所有Close个对象时始终致电DisposeFileStream

如果文件被其他进程打开(例如由另一个用户打开),那么您可以等待一段时间并重试移动文件。

相关问题