繁体   English   中英

System.IO.IOException:进程无法访问由System.IO .__ Error.WinIOError使用的文件

[英]System.IO.IOException: The process cannot access the file being used by System.IO.__Error.WinIOError

问题:首先,我的“创建”控制器操作方法以两种不同的方式创建两个文件。 但是,我的程序无法删除使用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;
    }

}

这是我的Delete controller动作的摘录://从FileSystem删除大小文件
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 Worker进程中打开了文件。关闭文件,然后重试。 如果我关闭VS并返回,则可以删除它,但是当我在Godaddy服务器上部署所有内容时,这对我没有帮助。

这是总消息:删除异常。 详细信息如下:System.IO.IOException:该进程无法访问文件'C:\\ aspnet4_cs \\ Pettigoats \\ Pettigoats \\ Images \\ demo \\ gallery \\ WalkingOnPorch.jpg',因为该文件正在被另一个进程使用。 在System.IO.File.InternalDelete(字符串路径,布尔值CheckHost)在System.IO.File.Delete(字符串路径)在Pettigoats.Controllers.CMAdminController.Delete在System.IO.File.InternalDelete(字符串路径,布尔值CheckHost) (int32 id)在c:\\ aspnet4_cs \\ Pettigoats \\ Pettigoats \\ Controllers \\ CMAdminController.cs:第53行

GDI +对该文件设置了锁定,以获取更多信息GDI +图形

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM