简体   繁体   English

该进程无法访问该文件,因为它正被另一个进程asp.net c#使用?

[英]The process cannot access the file because it is being used by another process asp.net c#?

I am trying to delete an image from a junk folder after uploading and resizing the image to my server folder. 我上传并调整图像大小到我的服务器文件夹后,我试图从垃圾文件夹中删除图像。 But then I am receiving this message which is not letting me delete the image from the junk folder. 但后来我收到这条消息,不让我从垃圾文件夹中删除图像。 How do I resolve this issue? 我该如何解决这个问题?

if (FileUpload1.HasFile)
{
    long fileSize = FileUpload1.FileContent.Length;
    double sizeinBytes = fileSize * 0.001;
    FileUpload1.SaveAs(Server.MapPath("~/junk/" + FileUpload1.FileName));
    string filepath = Server.MapPath("~/junk/" +FileUpload1.FileName);

    System.IO.FileStream fs = System.IO.File.OpenRead(filepath);
    byte[] data = new byte[fs.Length];
    fs.Read(data, 0, data.Length);

    System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
    Bitmap resizedimage = ResizeImage(image, 500, 500);
    resizedimage.Save(Server.MapPath("~/images/" + FileUpload1.FileName + ".jpeg"));
    Image1.ImageUrl = "~/images/" + FileUpload1.FileName;
    var filePath = Server.MapPath("~/junk/" + FileUpload1.FileName);
    if (File.Exists(filePath))
    {
        File.Delete(filePath);
    }
}

You have to encapsulate your Filestream like below, so that it gets disposed when finished: 您必须像下面一样封装您的文件流,以便在完成时将其处理:

using(FileStream fs = System.IO.File.OpenRead(filepath))
{
   //do stuff
}

//delete
if (FileUpload1.HasFile)
            {
                long fileSize = FileUpload1.FileContent.Length;
                double sizeinBytes = fileSize * 0.001;
                FileUpload1.SaveAs(Server.MapPath("~/junk/" + FileUpload1.FileName));
                string filepath = Server.MapPath("~/junk/" + FileUpload1.FileName);

                using (System.IO.FileStream fs = System.IO.File.OpenRead(filepath))
                {
                    byte[] data = new byte[fs.Length];
                    fs.Read(data, 0, data.Length);

                    System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                    Bitmap resizedimage = ResizeImage(image, 500, 500);
                    resizedimage.Save(Server.MapPath("~/images/" + FileUpload1.FileName + ".jpeg"));
                }
                Image1.ImageUrl = "~/images/" + FileUpload1.FileName;
                var filePath = Server.MapPath("~/junk/" + FileUpload1.FileName);
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }

暂无
暂无

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

相关问题 Asp.Net文件流IO异常:由于另一个进程正在使用该进程,因此无法访问文件 - Asp.Net File Stream IO Exception : Cannot access the file because the process is being used by another process ASP.NET Web Api 2 - Internet Explorer:该进程无法访问该文件,因为它正被另一个进程使用 - ASP.NET Web Api 2 - Internet Explorer: The process cannot access the file because it is being used by another process ASP.net该进程无法访问该文件,因为该文件正在被另一个进程使用 - ASP.net The process cannot access the file because it is being used by another process C# .NET BackgroundWorker 6 FileSystemWatcher 该进程无法访问文件“PATH”,因为它正被另一个进程使用 - C# .NET BackgroundWorker 6 FileSystemWatcher The process cannot access the file "PATH" because it is being used by another process c#无法访问文件,因为它正被另一个进程使用 - c# Cannot access file because it is being used by another process ASP.net c#文件被另一个进程使用 - ASP.net c# File being used by another process 进程无法访问文件,因为它被另一个进程使用 - c# - Process cannot access file because it is used by another process IOException:该进程无法访问文件“文件路径”,因为它正在被C#控制台应用程序中的另一个进程使用 - IOException: The process cannot access the file 'file path' because it is being used by another process in Console Application in C# C# File.ReadAllText 进程无法访问该文件,因为它正被另一个进程使用 - C# File.ReadAllText The process cannot access the file because it is being used by another process 生成 txt 文件时出现“进程无法访问文件 --- 因为它正被另一个进程使用” C# - "The process cannot access the file --- because it is being used by another process" when making txt file C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM