C# System.IO.IOException:进程无法访问System.IO正在使用的文件。\uu Error.WinIOError

C# System.IO.IOException:进程无法访问System.IO正在使用的文件。\uu Error.WinIOError,c#,asp.net,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc 4,问题:首先,我的“创建”控制器操作方法以两种不同的方式创建两个文件。但是,我的程序无法删除使用file.SaveAs(path)创建的文件; 但是,我可以成功删除使用创建的其他文件 保存(smallImageFilePath,System.Drawing.Imaging.ImageFormat.Jpeg) 下面是我的Create controller操作方法的HttpPost重载,包括ScaleByPercent方法调用:(在底部粘贴完整的错误消息) 以下是我的删除控制器操作的摘录: //从文件

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

下面是我的Create controller操作方法的HttpPost重载,包括ScaleByPercent方法调用:(在底部粘贴完整的错误消息)

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

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

“正在使用的文件” “无法完成该操作,因为该文件已在IIS工作进程中打开。请关闭该文件,然后重试。” 如果我关闭VS并返回,我可以删除它,但这对我在Godaddy服务器上部署所有内容都没有帮助

以下是全部信息:
删除异常。详细信息如下:System.IO.IOException:进程无法访问文件“C:\aspnet4\u cs\Pettigoats\Pettigoats\Images\demo\gallery\WalkingOnPorch.jpg”,因为其他进程正在使用该文件。在System.IO.File.System.IO.IO.Error.WinIOError(Int32 errorCode,String maybeFullPath)在System.IO.File.Delete(String path,Boolean checkHost)在Pettigoats.Controllers.CMAdminControllers.Delete(String path)在c:\aspnet4\Pettigoats\Pettigoats\Controllers\CMAdminControllers.cs中删除(Int32 id):第53行

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


保存后的Dispose()可能会起作用

具体来说,是
.FromFile
锁定了文件。如果您不想锁定它,您应该打开一个流,然后从流中加载映像。4nis感谢您的提醒,但是代码中当前存在一个Dispose()。请参见imgPhoto.Dispose();为了解决这个问题,我打开并加载了一条流,就像布拉德利·乌夫纳所说的那样。不过,感谢你们两位的快速回复。
    [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.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");
        }