2012-04-27 13 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は、ドライブ内のすべてのファイルとサブディレクトリのサイズをチェック

を次のように私は、ファイルsysteウォッチャーを作成しました。

これは、ディレクトリeq a.xls、.txtなどのファイルにファイルを追加するとうまく動作しますが、ディレクトリにフォルダを追加してもOnChangedイベントは発生しません。私は

watcher.IncludeSubdirectories = true; 

を有効にした場合

それはonChangedイベントイベントをトリガんが、この場合にはそれが唯一のサブディレクトリではなくディレクトリ全体をチェックします。

誰かが私はこれは私がディレクトリにフォルダをコピーするとき、それはonChangedイベントイベントをトリガ監視されていることを、このような仕事を得ると

私CalculateFolderSize機能のようにあるディレクトリの新しいサイズを計算することができますどのように私に言うことができるしてくださいこれはあなたがFileSystemEventArgsが提供するフォルダのパスを使用している

//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

ように変更されているフォルダになります場合に役立ちます、以下。代わりに、監視しているディレクトリルートごとにオブジェクトを作成し、ルートパスを保存してEventArgsの代わりにそのオブジェクトを使用します。

このオブジェクトを作成する簡単な方法は、次のようにイベントハンドラにラムダ関数を使用することです。これは、監視している各パスに対して、外側のスコープから別のオブジェクトにパスを効果的にラップします。

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

これは私がwatcher.Changed + =(送信者、E)=>を置いたときにそれはそれは無効な式の用語のヘルプであることを述べている素晴らしい解決策のように思えるし、それが動作することを確認イムしかし、私はVS C#に新しいですpls – mayukh

+0

私はまた置く;ラムダ式の最後では動作しません – mayukh

+1

どのバージョンのC#を使用していますか?ラムダ式を使用するには、バージョン3.0以降が必要です。 –

関連する問題