.net 删除图片,麻烦高手帮看一下
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.IOException: The process cannot access the file 'D:\wwwroot\20120604160136.jpg' because it is being used by another process.
Source Error:
Line 44: {
Line 45: Dispose();
Line 46: System.IO.File.Delete(Server.MapPath(filename));
=====================================================================================================
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["imageurl"] != null)
{
string lsrUlr = Request.QueryString["imageurl"].ToString();
Image1.ImageUrl = lsrUlr;
Label1.Text = lsrUlr;
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath(lsrUlr));
int ow = originalImage.Width;
int oh = originalImage.Height;
Label2.Text = ow.ToString();
Label3.Text = oh.ToString();
originalImage.Clone();
originalImage.Dispose();
}
}
protected void lbUploadPhoto_Click(object sender, EventArgs e)
{
fileUpload();
}
public void fileUpload()
{
if (fuPhoto.PostedFile != null && fuPhoto.PostedFile.ContentLength > 0)
{
string filename =Label1.Text.ToString();
try
{
Dispose();
System.IO.File.Delete(Server.MapPath(filename));
fuPhoto.PostedFile.SaveAs(Server.MapPath(filename));
fuPhoto.Dispose();
Response.Write("<script lanuage=javascript>alert('恭喜您,替换成功');</script>");
Response.End();
}
catch (InvalidCastException tsg)
{
Response.Write("<script lanuage=javascript>alert('上传失败,原因" + tsg + "');location='javascript:history.go(-1)'</script>");
Response.End();
}
}
}