开发者

Programmatically move files after virus scan

Is it possible to move files programmatically based on virus scan status?

What I want to do is have a set of folders:

Incoming Scanned Scanned/Clean Scanned/Infected Not开发者_StackOverflow Scanned

Files would be dropped into the Incoming folder. At that point, I would like to kick off the antivirus and scan the files in the Incoming folder. Once complete, the files would then need to be moved to the appropriate folder, either Clean or Infected. If, for whatever reason, the file could not be scanned or had trouble scanning, it would be moved to the Not Scanned folder.

I was hoping there would be a way to script this out. Has anyone ever done anything like this before?


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();
                }
            }
        }
    }
}

I created this as a Windows Service. My OnStart method creates my FileSystemWatcher to watch the Upload Path. For On Created, I have a method that runs my Scan method and creates my counter and sets it to 0. My On Error event just logs. I had an issue where the FileSystemWatcher was trying to open the file before it had been uploaded, hence why I added the sleep.

Finally, I am using Microsoft Forefront's command line scanner. File path: C:\Program Files\Microsoft Security Client\mpcmdrun.exe.

Let me know if any questions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜