我创建了一个使用对象FileSystemWatcher的Web服务。现在当我尝试打开文件时,服务自动停止,否则filecreated方法正常工作。这是我的代码,
protected override void OnStart(string[] args)
{
string dirname = @"C:\Users\Admin\Documents\Visual Studio 2017\WebSites\Project1\client";
watch = new System.IO.FileSystemWatcher(dirname, "*.*");
watch.Created += new FileSystemEventHandler(this.FileCreated);
watch.Changed += new FileSystemEventHandler(this.FileChanged);
watch.Deleted += new FileSystemEventHandler(this.FileDeleted);
watch.Renamed += new RenamedEventHandler(this.FileRenamed);
watch.IncludeSubdirectories = true;
watch.EnableRaisingEvents = true;}
和filechanged方法就是这个 -
public void FileChanged(object sender, FileSystemEventArgs e1)
{
MailMessage message = new MailMessage("***@gmail.com", "***@gmail.com");
Attachment attachfile = new Attachment(e1.FullPath);
message.Subject = "Data Changed " + e1.Name.ToString();
message.IsBodyHtml = true;
message.Body = "The Following File is Changed " + Path.GetFileName(e1.FullPath.ToString()) + " in " + " at " + DateTime.Now.ToLongTimeString() + " by " + System.Environment.UserName.ToString();
message.Attachments.Add(attachfile);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "email@gmail.com";
credentials.Password = "password";
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(message);
foreach (Attachment attachment in message.Attachments)
{
attachment.Dispose();
}
}
所以,请帮助我,即使我打开文件并更改其数据,如何继续运行Web服务。