2012-10-04 50 views
6

我正在使用下面的代码在内容的本地驱动器中创建一个文件。该进程无法访问该文件,因为它正在被另一个进程使用

File.WriteAllLines(path, contents); 

我将此文件附加到邮件并发送给团队。一旦邮件发送我需要删除的文件,删除我使用下面的代码的文件,但我得到的运行时错误

File.Delete(path); 

错误消息:该进程无法访问该文件,因为它正在使用另一个过程

默认情况下,WriteAllLines()方法关闭文件,但它仍然由另一个进程打开。我只能在某段时间之后通过执行代码来删除文件,但这不是这种情况。邮件发送后我需要删除它。

更新

System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); 

     mailMessage.To.Add(new System.Net.Mail.MailAddress(recipient, ToName)); 

     mailMessage.From = new System.Net.Mail.MailAddress(From, FromName); 
     mailMessage.Subject = Subject; // "Outlook calendar as attachment"; // modified by Srikanth J on 28/06/2012 
     mailMessage.Body = "This is a test message"; 

     System.Net.WebClient webclient = new System.Net.WebClient(); 

     webclient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 

     for (int i = 0; i < item.Attachments.Count; i++) 
     { 
      string url = item.Attachments.UrlPrefix + item.Attachments[i]; 
      SPFile file = item.ParentList.ParentWeb.GetFile(url); 
      mailMessage.Attachments.Add(new System.Net.Mail.Attachment(file.OpenBinaryStream(), file.Name)); 

     } 

     System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(path); 

     mailMessage.Attachments.Add(mailAttachment); 
     smtp.Send(mailMessage); 

任何帮助appriciated,谢谢。

+1

似乎将文件附加到邮件的过程仍然保存文件? –

+0

你可以发布附加文件的代码吗? @CuongLe建议,这很可能是问题所在。 –

+0

@CuongLe你的意思是即使在发送邮件之后,它仍然由邮件进程持有? – Srikanth

回答

14

MailMessage实现IDisposable,所以你应该使用using关键字,当你用它做释放任何资源。如果你不这样做,是的,这个文件很可能会一直保持使用,直到垃圾收集器发现你不再使用该消息。

using (var mailMessage = new MailMessage()) 
{ 
    // set mailMessage properties 
    // ... 
    smtp.Send(mailMessage); 
} 

您也可以打电话Dispose直接附着,但消息的配置就已经确保所有子对象,包括附件,得到正确配置。

0

编辑

try 
{ 
    //send mail code 
} 
finally 
{ 
    mailAttachment=null; 
    mailMessage=null; 
} 
//delete file code 

我以后sendind邮件表明,在最后你relese文件句柄或者如果你想这样做,关闭文件

try 
{ 
    //attach file 
    //send mail 
} 
finally 
{ 
    //set finally handle to null 
    // or close the file 
} 

//after this delete the file 
+2

'使用'会更好。 –

+0

@ L.B - 是的,好主意,在这里使用它,我刚刚粘贴sudo代码,因为op doent提供了任何代码 –

1

,我建议写你的文件将文件转换为流而不是文件。

这样,您可以完全避免文件锁定问题。

下面是一个演示如何基于流设置附件的示例(此处未显示用于编写流本身的代码)。

private static void SendReport(Report report) 
{ 
    MailMessage msg = new MailMessage 
    { 
     From = new MailAddress(Configuration.EmailFrom), 
     Subject = Configuration.EmailSubject, 
     Body = Configuration.EmailBody 
    }; 

    msg.To.Add(Configuration.EmailTo); 

    if (!string.IsNullOrWhiteSpace(Configuration.EmailCC)) 
     msg.CC.Add(Configuration.EmailCC); 

    if (!string.IsNullOrWhiteSpace(Configuration.EmailBcc)) 
     msg.Bcc.Add(Configuration.EmailBcc); 

    Program.AttachReport(report, msg); 

    SmtpClient smtp = new SmtpClient(); 
    smtp.Send(msg); 
} 

private static void AttachReport(Report report, MailMessage message) 
{ 
    Stream stream = new MemoryStream(); 
    report.Save(stream, Configuration.SurveyName); 
    message.Attachments.Add(new Attachment(stream, Configuration.AttachmentName, Configuration.AttachmentMediaType)); 
} 
2

只需添加一个简单的使用语句将

System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); 

这样

using(System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage()) 
{ 
     ....... 
     smtp.Send(mailMessage); 
} 

改变时,从使用语句代码退出MAILMESSAGE对象将每个对象设置以及实现IDisposable接口。这意味着,附件集合中的每个附件都将被丢弃,并且您的文件不再被锁定。

1

试试这个

File.WriteAllLines(path, contents); 
using(System.Net.Mail.MailMessage mailMessage 
           = new System.Net.Mail.MailMessage()) 
{ 
// send mail containing the file here 
} 
File.Delete(path); 

希望这有助于ü。

相关问题