2012-04-27 95 views
2

嗨我正在創建一個Windows服務來觀察某些目錄,看看目錄的大小是否達到其限制。 我創建了一個檔案系統觀察家如下FileSystemWatcher來監視目錄大小

  FileSystemWatcher watcher = new FileSystemWatcher(); 
      watcher.Path = dirPaths[i].ToString(); 
      watcher.NotifyFilter = NotifyFilters.Size; 
      watcher.EnableRaisingEvents = true; 
      watcher.Changed += new FileSystemEventHandler(OnChanged); 

private void OnChanged(object source, FileSystemEventArgs e) 
    { 
     try 
     { 


     string directory = new DirectoryInfo(e.FullPath).Parent.FullName;//gettting the directory path from the full path 

     float dirSize = CalculateFolderSize(directory); 

     float limitSize = int.Parse(_config.TargetSize);//getting the limit size 


     if (dirSize > limitSize) 
     { 
      eventLogCheck.WriteEntry("the folloing path has crossed the limit " + directory); 
      //TODO: mail sending 
     } 
    } 
    catch (Exception ex) 
    { 
     eventLogCheck.WriteEntry(ex.ToString()); 
    } 

} 

CalculateFolderSize檢查在驅動器中的所有文件和子目錄的大小。

現在,當我將文件添加到目錄EQ一個的.xls和.txt等文件,但如果我將文件夾添加到目錄它不會觸發給onChanged事件能正常工作?

如果我啓用

watcher.IncludeSubdirectories = true; 

它並觸發給onChanged事件,但在這種情況下,只檢查子目錄,而不是整個目錄。

請有人可以告訴我怎樣才能得到這個工作,這樣當我的文件夾複製到該目錄正在看着它觸發給onChanged事件並計算目錄

我CalculateFolderSize功能爲一體的新的大小如下如果這有助於

//function to calculate the size of the given path 
     private float CalculateFolderSize(string folder) 
     { 
      float folderSize = 0.0f; 
      try 
      { 
       //Checks if the path is valid or not   
       if (!Directory.Exists(folder)) 
       { 
        return folderSize; 
       } 
       else 
       { 
        try 
        { 
         foreach (string file in Directory.GetFiles(folder)) 
         { 
          if (File.Exists(file)) 
          { 
           FileInfo finfo = new FileInfo(file); 
           folderSize += finfo.Length; 
          } 
         } 
         foreach (string dir in Directory.GetDirectories(folder)) 
         { 
          folderSize += CalculateFolderSize(dir); 
         } 
        } 
        catch (NotSupportedException ex) 
        { 
         eventLogCheck.WriteEntry(ex.ToString()); 
        } 
       } 
      } 
      catch (UnauthorizedAccessException ex) 
      { 
       eventLogCheck.WriteEntry(ex.ToString()); 
      } 
      return folderSize; 
     } 

回答

5

您正在使用由FileSystemEventArgs提供的文件夾路徑,以便將已更改的文件夾。相反,請爲您正在監視的每個目錄根目錄創建一個對象,並在其中存儲根路徑並使用該對象而不是EventArgs

您可能會發現一個簡單的方法來創建這個對象就是使用lambda函數爲事件處理程序如下。這有效地將外部範圍的路徑封裝到您正在監視的每個路徑的不同對象中。

FileSystemWatcher watcher = new FileSystemWatcher(); 
watcher.Path = dirPaths[i].ToString(); 
watcher.NotifyFilter = NotifyFilters.Size; 
watcher.EnableRaisingEvents = true; 
watcher.Changed += delegate (object source, FileSystemEventArgs e) 
{ 
    float dirSize = CalculateFolderSize(watcher.Path); // using the path from the outer scope 

    float limitSize = int.Parse(_config.TargetSize);//getting the limit size 

    if (dirSize > limitSize) 
    { 
     eventLogCheck.WriteEntry("the folloing path has crossed the limit " + directory); 
     //TODO: mail sending 
    } 
}; 
+0

這似乎是一個很好的解決方案和IM確保它的工作原理,但我是新來的VS C#,所以當我把watcher.Changed + =(發件人,E)=>它說,它是一個無效的表達式項幫助請問 – mayukh 2012-04-27 09:13:17

+0

我也放了一個;在lambda表達式的末尾,但它不起作用 – mayukh 2012-04-27 09:25:27

+1

您使用的是什麼版本的C#?您需要3.0版或更高版本才能使用lambda表達式。 – 2012-04-27 09:26:12