现在老板让我们做一个程序实现监视目录,并定时11点发邮件给相应的人,每个目录对应的邮件接收者
如d:\share\is 对应的收件者为[email protected] [email protected] ,d:\share\mj 对应收件者为[email protected] [email protected], 目录是动态的,接收邮件的也是动态的,请问如何实现
如d:\share\is 对应的收件者为[email protected] [email protected] ,d:\share\mj 对应收件者为[email protected] [email protected], 目录是动态的,接收邮件的也是动态的,请问如何实现
{
internal string directory; //属性 被监视的目录
string msgs="";
internal string mailto; //属性 发送对象
internal string mailfrom;
FileSystemWatcher scan = new FileSystemWatcher(); //创建 filesystemwatcher类用于目录监视
internal void mailtoadd(string msg) //添加邮件接收人方法
{
mailto = mailto + msg + ',';
}
internal void start() //开始监控
{
scan.Path = directory; //被监视目录
scan.Filter = "*.*";
scan.NotifyFilter= NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
if (File.Exists(scan.Path ))
{
scan.EnableRaisingEvents = true;
scan.Changed += new FileSystemEventHandler(scan_Changed ); //创建事件
scan.Created += new FileSystemEventHandler(scan_Created);
scan.Renamed += new RenamedEventHandler(scan_Renamed);
scan.Deleted += new FileSystemEventHandler(scan_Deleted); //创建事件
} } private void scan_Changed(object sender, FileSystemEventArgs e) //监视对像修改事件
{
msgs = msgs + (DateTime.Now.ToString() + " " + e.Name + " " + "被修改!\r\n");
}
private void scan_Deleted(object sender, FileSystemEventArgs e) //监视文件删除事件
{
msgs = msgs + DateTime.Now.ToString() + " " + e.Name + " " + "被修改!\r\n";
}
private void scan_Renamed(object sender, FileSystemEventArgs e) //监视对象改名事件
{
msgs = msgs + DateTime.Now.ToString() + " " + e.Name + " " + "被改名!\r\n";
}
private void scan_Created(object sender, FileSystemEventArgs e) //监视对象创建事件
{
msgs = msgs + DateTime.Now.ToString() + " " + e.Name + " " + "被创建!\r\n";
}
internal void sendmail()
{
if (mailto.Length != 0)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(mailfrom ,mailto);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
mail.Body = msgs;
mail.Subject = directory + "目录监视";
smtp.Send(mail);
} }
}
类我已经写好了! 但我不知道如何动态创建它的实例! 因为要监控的目录是动态的!