Exemple #1
0
        /// <summary>
        /// Asynchronously scans a file for viruses.
        /// </summary>
        /// <param name="engine">ClamAV engine instance.</param>
        /// <param name="path">Path to the file to be scanned.</param>
        /// <param name="options">Scan options.</param>
        /// <returns>The task object representing the asynchronous operation. The Result property on the task returns a scan result.</returns>
        public static async Task <FileScanResult> ScanFileAsync(this ClamEngine engine, string path, ScanOptions options)
        {
            var virusName  = string.Empty;
            var scanResult = await Task.Factory.StartNew(() => engine.ScanFile(path, options, out virusName));

            return(new FileScanResult(path, scanResult == ScanResult.Virus, virusName));
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            using (ClamEngine e = new ClamEngine())
            {
                foreach (string file in args)
                {
                    ClamResult result = e.ScanFile(file);                     //pretty simple!

                    if (result != null && result.ReturnCode == ClamReturnCode.CL_VIRUS)
                    {
                        Console.WriteLine("Found: " + result.VirusName);
                    }
                    else
                    {
                        Console.WriteLine("File Clean!");
                    }
                }
            }             //engine is disposed of here and the allocated engine freed

            //these test clamd bindings
            using (ClamdSession session = new ClamdSession("127.0.0.1", 3310))
            {
                using (ClamdManager manager = new ClamdManager(session))
                {
                    Console.WriteLine(manager.GetVersion());
                    Console.WriteLine(manager.ScanWithArchiveSupport("/home/bperry/tmp"));
                }
            }
        }
Exemple #3
0
 private void Scan()
 {
     for (int i = 0; i <= files.Count - 1; i++)
     {
         string     virusName;
         ScanResult result = clamAV.ScanFile(files[i], ScanOptions.StandardOptions, out virusName);
         this.Invoke(new Action(() =>
         {
             // progressBar1.Value = i;
             //LblCurrentScan.Text = files[i];
         }));
     }
 }
Exemple #4
0
        public static void Main(string[] args)
        {
            using (ClamEngine e = new ClamEngine())
            {
                foreach (string file in args)
                {
                    ClamResult result = e.ScanFile(file);                     //pretty simple!

                    if (result != null && result.ReturnCode == ClamReturnCode.CL_VIRUS)
                    {
                        Console.WriteLine("Found: " + result.VirusName);
                    }
                    else
                    {
                        Console.WriteLine("File Clean!");
                    }
                }
            }             //engine is disposed of here and the allocated engine freed automatically
        }
        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();
            }
        }