上传后如何删除文件 [英] How I Delete File after uploading
本文介绍了上传后如何删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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("email.testitbd@gmail.com", "1234ASDF");
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();
}
当我这样做时会引发异常,例如
when i did this it throws exception like
An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code
Additional information: The process cannot access the file 'C:\Users\User\Documents\Visual Studio 2013\WebSites\Email_Sent_Web\Upload_File\DMP.edx' because it is being used by another process.
我如何解决plz帮助,感谢Advance
how i solve plz help ,thanks in Advance
推荐答案
同时处理dependecy对象
Dispose the dependecy objects as well
SmtpServer.Send(mail);
data.Dispose();
data = null;
email_attach.Dispose();
email_attach = null;
这篇关于上传后如何删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文