2013-10-24 26 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