因为它正由另一个进程的进程无法访问文件 - 使用静态类 [英] The process cannot access the file because it is being used by another process - using static class

查看:90
本文介绍了因为它正由另一个进程的进程无法访问文件 - 使用静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的错误该进程无法访问该文件,因为它正由另一个进程当我试图删除我刚刚上传的文件(这是文件后至少几秒钟被上传,所以我确保他们写完)。任何想法,为什么出现这种情况? PS:我生成缩略图删除没有问题,但原稿莫名其妙地锁定。

  VAR FileExt = Path.GetExtension(photo.File.FileName);
            VAR文件路径= Path.Combine(使用Server.Mappath(〜/ App_Data文件/+ photo.ClientId),photo.PhotoId.ToString())+ FileExt;
            photo.File.SaveAs(文件路径);
            变种ThumbFilePath = Path.Combine(使用Server.Mappath(〜/ App_Data文件/+ photo.ClientId),photo.PhotoId.ToString()+_thumbnail)+ FileExt;
            PhotoTools.MakeThumbnail(文件路径,ThumbFilePath,0.15);
            返回RedirectToAction(创建);

里面一个光工具类...

 公共静态无效MakeThumbnail(字符串ImgIn,串ImgOut,双百分号)
    {
        图片IMG = Image.FromFile(ImgIn);
        双宽度= img.Width *百分比;
        双高= img.Height *百分比;
        MakeThumbnail(ImgIn,ImgOut,(INT)宽,(INT)高度);
    }

删除功能...

 公众的ActionResult DeleteConfirmed(INT ID)
    {
        Client客户端= db.Clients.Find(ID);
        db.Clients.Remove(客户端);
        db.SaveChanges();
        如果(Directory.Exists(使用Server.Mappath(〜/ App_Data文件/+ ID)))
        {
            Directory.Delete(使用Server.Mappath(〜/ App_Data文件/)+ ID,真实);
        }
        返回RedirectToAction(「指数」);
    }


解决方案

在您MakeThumbnail方法,确保你调用的Dispose()图片类型。这或使用使用语法:

 使用(图片IMG = Image.FromFile(ImgIn))
{
    //你的code
}

I get the error "The process cannot access the file because it is being used by another process" when I attempt to delete the files I just uploaded (this is at least a few seconds after the files were uploaded, so I am sure they are finished writing). Any ideas why this occurs? PS: The thumbnails I generate delete without a problem, but the originals are locked somehow.

            var FileExt = Path.GetExtension(photo.File.FileName);
            var FilePath = Path.Combine(Server.MapPath("~/App_Data/" + photo.ClientId), photo.PhotoId.ToString()) + FileExt;
            photo.File.SaveAs(FilePath);
            var ThumbFilePath = Path.Combine(Server.MapPath("~/App_Data/" + photo.ClientId),photo.PhotoId.ToString() + "_thumbnail") + FileExt;
            PhotoTools.MakeThumbnail(FilePath, ThumbFilePath, 0.15);
            return RedirectToAction("Create");

Inside a PhotoTools class...

    public static void MakeThumbnail(string ImgIn, string ImgOut, double Percent)
    {
        Image img = Image.FromFile(ImgIn);
        double Width = img.Width*Percent;
        double Height = img.Height*Percent;
        MakeThumbnail(ImgIn, ImgOut, (int)Width, (int)Height);
    }

Delete function...

    public ActionResult DeleteConfirmed(int id)
    {
        Client client = db.Clients.Find(id);
        db.Clients.Remove(client);
        db.SaveChanges();
        if (Directory.Exists(Server.MapPath("~/App_Data/" + id)))
        {
            Directory.Delete(Server.MapPath("~/App_Data/") + id,true);
        }
        return RedirectToAction("Index");
    }

解决方案

In your MakeThumbnail methods, make sure you're calling Dispose() on the Image types. That or use the using syntax:

using (Image img = Image.FromFile(ImgIn))
{
    // Your code
}

这篇关于因为它正由另一个进程的进程无法访问文件 - 使用静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆