【发布时间】:2013-10-25 15:02:08
【问题描述】:
我想发送一个在系统观察程序发现创建的以 ".txt"、".doc" 或 ".docx" 结尾的文件时找到的文件"。我的问题是系统观察程序只发现以“.txt”结尾的文件。
这是我的代码:
private String Attachmenttosend
{
get { return attachmentfile; }
set { attachmentfile = value; }
}
private void NewFileSystemWatcher()
{
String filter = "*.txt,*.doc,*.docx";
string[] FileExtension = filter.Split(',');
for (int i = 0; i < FileExtension.GetLength(0); i++)
{
watcher = new FileSystemWatcher(folder); // on its own Thread
watcher.Created += new FileSystemEventHandler(NewEMail);
attachmenttosend.Add(Attachmenttosend);
watcher.Filter = FileExtension[i];
watcher.EnableRaisingEvents = true;
watchlist.Add(watcher);
}
Send(Attachmenttosend);
}
private void NewEMail(Object source, FileSystemEventArgs e)
{
while (Locked(e.FullPath)) // check if the file is used
{
Thread.Sleep(10);
}
Attachmenttosend = e.FullPath; // store the filename
}
【问题讨论】:
-
您想捕捉特定目录中任何文件的每一次修改吗?
-
如果创建了一个(或更多)文件(以 .txt、.doc 或 .docx 结尾),那么系统观察者是否应该发现该/那些文件并将文件名返回给我.它仅适用于 *.txt。就像在链接stackoverflow.com/questions/6965184/… 中一样,我为每个文件扩展名创建了一个 FileSystemWatcher 对象。
-
尝试注释掉新邮件中的附件行和while循环,并通过在NewEMail中打印来检查它是否返回事件。
-
这一行做了什么“attachmenttosend.Add(Attachmenttosend);”
标签: c# filesystemwatcher