Exemplo n.º 1
0
        private void ScanDir()
        {
            var scannedFiles = new List <Tuple <string, ScanResult, string> >();

            try
            {
                for (int i = 0; i <= selectedDir.Count - 1; i++)
                {
                    clamAV.ScanDirectory(selectedDir[i], (file, result, virus) =>
                    {
                        scannedFiles.Add(Tuple.Create(file, result, virus));
                        BeginInvoke(new MethodInvoker(delegate()
                        {
                            LblCurrent.Text   = file;
                            LblTotalScan.Text = scannedFiles.Count.ToString();
                        }));
                    });

                    var infected = scannedFiles.Where(f => f.Item2 == ScanResult.Virus);
                    BeginInvoke(new MethodInvoker(delegate()
                    {
                        LblDetected.Text = infected.Count().ToString();
                    }));
                }
            }
            catch { }
        }
Exemplo n.º 2
0
        private void scanButton_Click(object sender, EventArgs e)
        {
            string         scanPath = scanPathTextBox.Text;
            FileAttributes attribs  = File.GetAttributes(scanPath);

            if ((attribs & FileAttributes.Directory) == FileAttributes.Directory)
            {
                // Perform directory scan.
                Thread scanThread = new Thread(() =>
                {
                    var scannedFiles = new List <Tuple <string, ScanResult, string> >();

                    this.Invoke(new Action(() =>
                    {
                        scanProgressBar.Style = ProgressBarStyle.Marquee;
                        scanProgressBar.Value = 100;

                        logTextBox.AppendText("==========\r\n");
                        logTextBox.AppendText("\r\n");
                        logTextBox.AppendText("Scanning directory " + scanPath + "\r\n");
                    }));

                    _clamAV.ScanDirectory(scanPath,
                                          (file, result, virus) =>
                    {
                        scannedFiles.Add(Tuple.Create(file, result, virus));

                        this.Invoke(new Action(() =>
                        {
                            logTextBox.AppendText("Scanning: " + file + "\r\n");
                        }));
                    });

                    // Analyse results.
                    var infectedFiles = scannedFiles.Where(f => f.Item2 == ScanResult.Virus);

                    this.Invoke(new Action(() =>
                    {
                        logTextBox.AppendText("==========\r\n");
                        logTextBox.AppendText("\r\n");
                        logTextBox.AppendText(scanPath + " scanned\r\n");
                        logTextBox.AppendText("\r\n");
                        logTextBox.AppendText(string.Format("{0} file(s) scanned, {1} infected\r\n",
                                                            scannedFiles.Count, infectedFiles.Count()));
                        logTextBox.AppendText("\r\n");

                        foreach (var file in infectedFiles)
                        {
                            logTextBox.AppendText(string.Format("{0} infected with {1}\r\n", file.Item1, file.Item3));
                        }

                        logTextBox.AppendText("\r\n");

                        scanProgressBar.Style = ProgressBarStyle.Blocks;
                        scanProgressBar.Value = 100;
                    }));
                });

                scanThread.Start();
            }
            else
            {
                // Perform file scan.
                Thread scanThread = new Thread(() =>
                {
                    // Signal start of scan on UI thread.
                    this.Invoke(new Action(() =>
                    {
                        scanProgressBar.Style = ProgressBarStyle.Blocks;

                        statusLabel.Text      = "Scanning...";
                        scanProgressBar.Value = 10;
                    }));

                    string virusName = "";

                    ScanResult scanResult = _clamAV.ScanFile(scanPath, ScanOptions.StandardOptions, out virusName);

                    // Report scan completion.
                    this.Invoke(new Action(() =>
                    {
                        statusLabel.Text      = "Scan complete.";
                        scanProgressBar.Value = 100;

                        logTextBox.AppendText("==========\r\n");
                        logTextBox.AppendText("\r\n");
                        logTextBox.AppendText("Scanning file " + scanPath + " complete.\r\n");
                        if (scanResult == ScanResult.Clean)
                        {
                            logTextBox.AppendText("File is clean.\r\n");
                        }
                        else
                        {
                            logTextBox.AppendText("File is infected with " + virusName + ".\r\n");
                        }
                        logTextBox.AppendText("\r\n");
                    }));
                });

                // Begin scanning.
                scanThread.Start();
            }
        }