2011-08-15 55 views
0

是否可以基於病毒掃描狀態以編程方式移動文件?病毒掃描後以編程方式移動文件

我想要做的是有一組文件夾:

傳入 掃描 掃描/清潔 掃描/感染 未掃描

文件將被丟棄到輸入文件夾中。此時,我想啓動防病毒並掃描Incoming文件夾中的文件。一旦完成,這些文件就需要被移動到適當的文件夾,清潔或感染。如果由於某種原因無法掃描文件或遇到掃描問題,它將被移至未掃描文件夾。

我希望能有一種方法來編寫腳本。有沒有人曾經做過這樣的事情?

+0

當然,這是可能的。一切皆有可能。但這是一個相當廣泛的問題,你沒有指定你使用的AV,甚至沒有指定你正在運行的環境,所以你不可能得到任何有用的答案。 – Jason

+0

在這一點上......任何事情。我不在乎防病毒,它是一個簡單的Windows環境。這些文件通過POST放置到Windows Server 2003服務器上。如果我必須下載免費的防病毒軟件,我會的。爲了記錄,服務器運行Symantec。 – Matt

+0

這可能是更好的超級用戶問題 - 「哪個AV將允許我監視文件夾X並將受感染的文件移動到文件夾Y」 – Jason

回答

0
public void Scan() 
{ 
    string[] uploadPath = Directory.GetFiles(ConfigurationManager.AppSettings["UploadPath"]); 

    foreach(string filePath in uploadPath) 
    { 
     string fileName = Path.GetFileName(filePath); 
     string cleanPath = Path.Combine(ConfigurationManager.AppSettings["CleanPath"], fileName); 

     try 
     { 
      Process AV = new Process(); 

      AV.StartInfo.UseShellExecute = false; 
      AV.StartInfo.RedirectStandardOutput = true; 
      AV.StartInfo.FileName = ConfigurationManager.AppSettings["VSApp"]; 
      AV.StartInfo.Arguments = " -Scan -ScanType 3 -file " + ConfigurationManager.AppSettings["UploadPath"] + " -DisableRemediation"; 

      AV.Start(); 

      string output = AV.StandardOutput.ReadToEnd(); 
      AV.WaitForExit(); 

      if (AV.ExitCode == 0) 
      { 
       File.Move(filePath, cleanPath); 
      } 

      else if (AV.ExitCode == 2) 
      { 
       using (TextWriter tw = new StreamWriter(ConfigurationManager.AppSettings["FailedPath"] + fileName + ".txt")) 
       { 
        tw.WriteLine("2"); 
        tw.Close(); 
       } 

       using (TextWriter tw1 = new StreamWriter(ConfigurationManager.AppSettings["FailedFiles"] + fileName + ".txt")) 
       { 
        tw1.WriteLine(AV.StandardOutput); 
        tw1.Close(); 
       } 

       File.Delete(filePath); 
      } 

      AV.Close(); 
     } 

     catch (Exception ex) 
     { 
      if (ex.ToString().Contains("Could not find file")) 
      { 
       string failedFile = ConfigurationManager.AppSettings["FailedPath"] + fileName + ".txt"; 
       string failedFileDesc = ConfigurationManager.AppSettings["FailedPath"] + fileName + "_ErrorDesc" + ".txt"; 
       using (TextWriter tw = new StreamWriter(failedFile)) 
       { 
        tw.WriteLine("2"); 
        tw.Close(); 
       } 

       using (TextWriter tw1 = new StreamWriter(failedFileDesc)) 
       { 
        tw1.WriteLine(ex.ToString()); 
        tw1.Close(); 
       } 
      } 

      else 
      {      
       Thread.Sleep(2000); 
       if (runCounter == 0) 
       { 
        Scan(); 
       } 
       runCounter++; 

       string errorFile = ConfigurationManager.AppSettings["ProcessErrorPath"] + fileName + ".txt"; 
       using (TextWriter tw = new StreamWriter(errorFile)) 
       { 
        tw.WriteLine(ex.ToString()); 
        tw.Close(); 
       } 
      } 
     } 
    } 
} 

我將此創建爲Windows服務。我的OnStart方法創建我的FileSystemWatcher來觀察上傳路徑。對於創建,我有一個方法,運行我的掃描方法,並創建我的計數器,並將其設置爲0.我的錯誤事件剛剛記錄。我有一個FileSystemWatcher在上載之前試圖打開文件的問題,因此我添加了睡眠。

最後,我使用Microsoft Forefront的命令行掃描程序。文件路徑:C:\ Program Files \ Microsoft安全客戶端\ mpcmdrun.exe。

讓我知道是否有任何問題。

+0

,我記錄了失敗的代碼(2)和實際的失敗。失敗代碼被用作SQL中的標誌。我使用實際的失敗來弄清楚發生了什麼。在Forefront中,0表示通過,2表示失敗,我認爲它是未知的5。當文件在上傳之前打開或者剛剛離開時發生未知事件。 – Matt