在将图像上传到我的服务器文件夹并调整其大小后,我试图从垃圾文件夹中删除图像。但是后来我收到了这条消息,它不允许我从垃圾文件夹中删除图像。我该如何解决这个问题?
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);
}
}
最佳答案
您必须像下面这样封装您的文件流,以便在完成后将其处理掉:
using(FileStream fs = System.IO.File.OpenRead(filepath))
{
//do stuff
}
//delete
关于c# - 该进程无法访问该文件,因为它正被另一个进程 asp.net c# 使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42852576/