参考genearting Excel,然后我附上它作为附件邮寄,然后发送邮件mail.After发送我想删除生成的文件。在删除它抛出错误:如何删除Excel互操作对象,我使用EXCEL互操作在C#应用程序
this file cannot be deleted as it is is used by another process
我搜索了SO
等网站,发现有关COM组件有趣的事实。
参考: - How do I properly clean up Excel interop objects?
如果我不附加它作为附件然后文件被删除。在删除之前,我删除所有COM references.but当我使用它作为附件它抛出错误: 我的代码是这样的:
workbook.SaveAs(root + statics + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Garbage collecting
// Clean up references to all COM objects
// As per above, you're just using a Workbook and Excel Application instance, so release them:
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(m_objRange);
Marshal.FinalReleaseComObject(worksheet);
workbook.Close(false, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(workbook);
app.Quit();
Marshal.FinalReleaseComObject(app);
MailMessage mm = new MailMessage("[email protected]", "[email protected]", "TestMsg", "Hi");
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
//Commenting Below two lines works fine ....!!!!!!!!!....WHY..???
//Attachment data = new Attachment(root + statics + ".xls");
//mm.Attachments.Add(data);
client.Send(mm);
File.Delete(root + statics + ".xls");
如何安装它邮寄后删除引用。 感谢
你真的拥有所有代码在finally块吗? – Steve 2012-03-01 12:56:14
都能跟得上..它只是一个示例代码。 – Pranav 2012-03-01 13:00:26
这不是Excel中,它的附件 - 见http://stackoverflow.com/questions/5191449/file-locked-after-sending-it-as-attachement – dash 2012-03-01 13:10:18