在ASP.net FileUpload Control中上传后如何删除文件

时间:2014-02-01 06:20:27

标签: c# asp.net

string savePath = Server.MapPath("\\Upload_File\\");
// Get the name of the file to upload.
string fileName = email_attach.FileName;
string pathToCheck = savePath + fileName;


email_attach.SaveAs(pathToCheck);

string file = pathToCheck;
Attachment data = new Attachment(file);
mail.Attachments.Add(data);

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
email_attach.Dispose();
FileInfo file_Delete = new FileInfo(pathToCheck);
if (file_Delete.Exists)
{
    file_Delete.GetAccessControl();
    file_Delete.Delete();
}

当我这样做时会引发异常,如

  

mscorlib.dll中出现“System.IO.IOException”类型的异常,但未在用户代码中处理

     

其他信息:该进程无法访问文件“C:\ Users \ User \ Documents \ Visual Studio 2013 \ WebSites \ Email_Sent_Web \ Upload_File \ DMP.edx”,因为它正由另一个进程使用。

我如何解决plz帮助,谢谢你提前

1 个答案:

答案 0 :(得分:0)

尝试在data.Dispose();

下面写下mail.Attachments.Add(data);