2013-10-24 28 views
0

我想给当系统监控已discoverd与.TXT”,“ .DOC”或“的.docx”结束了创建的文件已被发现的文件。我的问题是系统监视器只发现以“ .txt”结尾的文件。FileSystemWatcher的:多个类型

这里是我的代码:

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 
} 
+4

可能的重复http://stackoverflow.com/questions/6965184/how-to-set-filter-for-filesystemwatcher-for-multiple-file-types –

+0

你想捕捉特定任何文件的每一个修改目录? – terrybozzio

+0

如果创建了一个文件(或更多)(endig用.txt,.doc或.docx),那么systemwatcher应该发现那个/这些文件并给我返回文件的名称。它仅适用于* .txt。像在链接http://stackoverflow.com/questions/6965184/how-to-set-filter-for-filesystemwatcher-for-multiple-file-types我为每个文件扩展名创建一个FileSystemWatcher对象。 – funk

回答

0

我认为这会帮助你,只是动态创建一个控制台应用程序,并粘贴在与尝试一下:

 private static string[] filters = new string[] { ".txt", ".doc", ".docx" }; 

     static void Main(string[] args) 
     { 
      FileSystemWatcher watcher = new FileSystemWatcher(); 
      watcher.Path = @"C:\...\...\...";//your directory here 
      /* Watch for changes in LastAccess and LastWrite times, and 
       the renaming of files or directories. */ 
      watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
       | NotifyFilters.FileName | NotifyFilters.DirectoryName; 

      //dont set this 
      //watcher.Filter = "*.txt"; 

      watcher.Changed += new FileSystemEventHandler(OnChanged); 
      watcher.Created += new FileSystemEventHandler(OnChanged); 
      watcher.Deleted += new FileSystemEventHandler(OnChanged); 
      watcher.Renamed += new RenamedEventHandler(OnRenamed); 

      watcher.EnableRaisingEvents = true; 
      Console.ReadKey(); 
     } 

     private static void OnChanged(object source, FileSystemEventArgs e) 
     {   
      if(filters.Contains(Path.GetExtension(e.FullPath))) 
      { 
       Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); 
       //Attachmenttosend = e.FullPath; 
      } 
     } 

     private static void OnRenamed(object source, RenamedEventArgs e) 
     { 
      if (filters.Contains(Path.GetExtension(e.FullPath))) 
      Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); 
     } 

另外,作为注:Kunal指出

attachmenttosend.Add(Attachmenttosend);

我想从大写和小写,你试图添加到属性的支持领域自己的属性,不,也...你不添加到只有字符串+ =(concat)。 除非attachmenttosend是一个例如字符串列表。

+0

attachmenttosend是列表:它确实添加系统监视器在使用扩展名“.txt,.doc或* .docx”创建文件时找到的文件名 – funk