I have a file, to which data is saved a all the time.. I have a timer which runs once every 4 second My process looks like: File x.txt is open all the time. after 4 sec I want to stop the process and send this file by email. I want to repeat above steps all the time.
my code:
var timer = new System.Timers.Timer(4000);
timer.Enabled = true;
timer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
{
wrtiteToFile.Suspend();
SendEmail();
wrtiteToFile.Resume();
};
static void SendEmail()
{
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("", "");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Port = ;
smtpClient.Host = "";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
smtpClient.EnableSsl = true;
message.From = fromAddress;
message.Subject = "";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "";
message.To.Add("");
message.Attachments.Add(new Attachment(Application.StartupPath + @"\log.txt"));
smtpClient.Send(message);
}
The process cannot access the file 'C:\log.txt' because it is being used by another process. It's not working. What is the best manner in which I can do this? I tried lock but it doesn't work too. I must use .net framework 2.0