病毒扫描后以编程方式移动文件

时间:2011-08-15 21:46:48

标签: antivirus

是否可以根据病毒扫描状态以编程方式移动文件?

我想要做的是拥有一组文件夹:

接收 扫描 扫描/清洁 扫描/感染 未扫描

文件将被放入Incoming文件夹中。此时,我想启动防病毒软件并扫描Incoming文件夹中的文件。完成后,需要将文件移动到相应的文件夹,Clean或Infected。如果由于某种原因无法扫描文件或扫描时出现问题,则会将其移至“未扫描”文件夹。

我希望有一种方法来编写这个。有没有人以前做过这样的事情?

1 个答案:

答案 0 :(得分:1)

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以观察上传路径。对于On Created,我有一个方法来运行我的Scan方法并创建我的计数器并将其设置为0. My On Error事件只是记录。我有一个问题,FileSystemWatcher在上传之前试图打开文件,因此我加入了睡眠。

最后,我使用的是Microsoft Forefront的命令行扫描程序。文件路径:C:\ Program Files \ Microsoft Security Client \ mpcmdrun.exe。

如果有任何问题,请告诉我。

相关问题