【问题标题】:System.IO.IOException: The process cannot access the file being used by System.IO.__Error.WinIOErrorSystem.IO.IOException:进程无法访问 System.IO.__Error.WinIOError 正在使用的文件
【发布时间】:2014-12-22 17:05:10
【问题描述】:

问题:首先,我的“创建”控制器操作方法以两种不同的方式创建两个文件。但是,我的程序无法删除使用 file.SaveAs(path); 创建的文件。 但是,我可以成功删除使用创建的其他文件 imgPhoto.Save(smallImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);

这是我的 Create 控制器操作方法的 HttpPost 重载,包括一个 ScaleByPercent 方法调用:(完整的错误消息粘贴在底部)

    [HttpPost]
    [Authorize]
    [ValidateAntiForgeryToken]
    public ActionResult Create(HttpPostedFileBase file, Models.Gallery gallerycm)
    {
        ViewBag.Message = "Testing Gallery File Create";

        if (file != null && file.ContentLength > 0)
            try
            {
                string path = Path.Combine(Server.MapPath("~/Images/demo/gallery"),
                                           Path.GetFileName(file.FileName));

                //System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal);

                //System.Drawing.Image MainImgPhotoVert = System.Drawing.Image.FromFile(path);
                /*
                System.Drawing.Image MainImgPhotoVert = System.Drawing.Image.FromStream(System.IO.Stream file);
                Bitmap MainImgPhoto = (System.Drawing.Bitmap)ScaleByPercent(MainImgPhotoVert, 100);
                MainImgPhoto.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
                MainImgPhoto.Dispose();
                */

                file.SaveAs(path);
                file.InputStream.Flush(); //useless
                file.InputStream.Close(); //less than useless
                file.InputStream.Dispose(); //complete waste of keystrokes

                //System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal);

                // Validating whether the following commented code releases a recently created
                // file from IIS for file Delete.  Problem occuring in the Visual Studio test environment.
                //file.InputStream.Dispose();
                //GC.Collect();
                //GC.WaitForPendingFinalizers();

                // Create the Thumbnail image
                string smallImageFilePath = Path.Combine(Server.MapPath("~/Images/demo/gallery/") + "ThumbSize" + (file.FileName));
                //allocate an Image object from the uploaded full sized .jpg 
                System.Drawing.Image imgPhotoVert = System.Drawing.Image.FromFile(path);
                Bitmap imgPhoto = (System.Drawing.Bitmap)ScaleByPercent(imgPhotoVert, 50);
                imgPhoto.Save(smallImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                imgPhoto.Dispose();

                var gallery = new Gallery();
                //gallery.PhotoNumberID = 9;
                gallery.Filename = file.FileName;
                if (gallerycm.PhotoDescription == null)
                    gallerycm.PhotoDescription = " ";
                gallery.PhotoDescription = gallerycm.PhotoDescription;

                var galleryContext = new EFDbGalleryContext();
                galleryContext.Gallery.Add(gallery);
                galleryContext.SaveChanges();
            }
            catch (Exception ex)
            {
                TempData["SomeData"] = file.FileName + " Upload exception.  The Details follow:  " + ex.ToString();
                return RedirectToAction("Index");
            }
        else
        {
            ViewBag.Message = "You have not specified a file.";
        }
        TempData["SomeData"] = "Photo was successfully Added";
        return RedirectToAction("Index");
    }


    static System.Drawing.Image ScaleByPercent(System.Drawing.Image imgPhoto, int Percent)
    {
        float nPercent = ((float)Percent / 100);

        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;
        int sourceX = 0;
        int sourceY = 0;

        int destX = 0;
        int destY = 0;
        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
                                 System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                                imgPhoto.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode =
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        return bmPhoto;
    }

}

这是我的删除控制器操作的摘录: //从文件系统中删除大小文件
System.IO.File.Delete(smallImageFilePath);

        //System.IO.File.GetAccessControl(largeImageFilePath);
        try 
        {
        System.IO.File.GetAccessControl(largeImageFilePath); // Does not help
        System.IO.File.Delete(largeImageFilePath);
        }
        catch (System.IO.IOException e)
        {
            TempData["SomeData"] = " Delete exception.  The Details follow:  " + e.ToString();
            return RedirectToAction("Index");
        }

另请注意: 如果我在 VS Debug 中暂停程序,我可以在 Windows 资源管理器中删除位图文件,但另一个返回:

“正在使用的文件” “操作无法完成,因为文件已在 IIS 工作进程中打开。关闭文件并重试。” 如果我关闭 VS 并重新进入,我可以将其删除,但是当我在 Godaddy 服务器上部署所有内容时,这对我没有帮助。

以下是全部信息: 删除异常。详细信息如下: System.IO.IOException:该进程无法访问文件 'C:\aspnet4_cs\Pettigoats\Pettigoats\Images\demo\gallery\WalkingOnPorch.jpg',因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.InternalDelete(String path, Boolean checkHost) 在 System.IO.File.Delete(String path) 在 Pettigoats.Controllers.CMAdminController.Delete (Int32 id) 在 c:\aspnet4_cs\Pettigoats\Pettigoats\Controllers\CMAdminController.cs:line 53

【问题讨论】:

    标签: c# asp.net asp.net-mvc-4


    【解决方案1】:

    GDI+ 锁定文件,更多信息GDI+ Graphics

    保存后的 Dispose() 可能会解决问题

    【讨论】:

    • 具体来说,锁定文件的是.FromFile。如果您不希望它被锁定,您应该打开一个流并从流中加载图像。
    • 4nis 感谢您的提醒,但代码中目前存在 Dispose()。见 imgPhoto.Dispose();以上
    • 为了解决问题,我打开并加载了一个流,正如 Bradley Uffner 指出的那样。不过,感谢你们两位的快速回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2021-01-05
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多