Hi
i have a file which comes from sql server and dumps into the location D:\Test_File, this file needed to be mailed to my mailid using the filesystemwatcher for this i have written a windows service and according to my logic when the file is created in the folder D:\Test_File my windows service will send the mail and it will delete that file and again when the file comes to that particular location it will again pick and send the mail then delete the file. My code is as fallows
protected override void OnStart(string[] args)
{
fileSystemWatcher1.Path =@"D:\Test_File\Test.html";
}
private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
MailMessage mail = new MailMessage();
mail.To = xyz@abc.com;
mail.From = def@abc.com;
mail.Subject = "Alerting You";
mail.BodyFormat = MailFormat.Html;
mail.Body = File.ReadAllText(@"D:\Test_File\Test.html", Encoding.UTF8);
mail.Attachments.Add(new MailAttachment(@"D:\Test_File\Test.html"));
SmtpMail.SmtpServer = "kptsc.vfstc.local";
SmtpMail.Send(mail);
File.Delete(@"D:\Test_File\Test.html");
}
but for the first time its working fine,but for the second time its throwing error as "The process cannot access the file 'D:\Test_File\Test.html' because it is being used by another process"