mCleaner.Logics.Clam.CommandLogic_Clam.LaunchScanner C# (CSharp) Method

LaunchScanner() public method

public LaunchScanner ( SEARCH search, string path, bool launchinbackground = false, string regex = null, bool remove = false, System.Action callback = null ) : void
search SEARCH
path string
launchinbackground bool
regex string
remove bool
callback System.Action
return void
        public void LaunchScanner(SEARCH search, string path, bool launchinbackground = false, string regex = null, bool remove = false, Action callback = null)
        {
            this.Clam.InfectedFilesCount = 0;
            this.Clam.WindowTitle = "Scan custom locations for viruses";
            this.Clam.EnableCleanNowButton = false;
            this.Clam.EnableCancelButton = true;
            this.Clam.EnableCloseButton = true;

            this.WriteConfig();

            List<string> param = new List<string>() {
                //"--tempdir \"{0}\"",
                //"--keep-mbox",
                //"--stdout",
                "--database=\"{1}\"",
                "--log=\"{2}\"",
                "--infected",
                "--recursive=yes"
            };

            if(IsRemove)
                param.Add("--remove=yes");

            // if max files are declared
            StringCollection max = Settings.Default.ClamWin_Max;
            string[] limits  = {
                                  "--max-files=" + max[0],
                                  "--max-scansize=" + max[1],
                                  "--max-recursion=" + max[2],
                                  "--max-filesize=" + max[3]
                              };

            // exclusions
            string exclude = string.Empty;
            List<string> list_exclude = new List<string>();
            foreach (string e in Settings.Default.ClamWin_Exclude)
            {
                list_exclude.Add(string.Format("--exclude=\"{0}\"", e));
            }
            exclude = string.Join(" ", list_exclude.ToArray());

            // add additional parameters
            if (regex != null)
            {
                param.Add("--include=\"" + regex + "\"");
            }
            param.Add(string.Join(" ", limits));

            switch (search)
            {
                case SEARCH.clamscan_file:
                    param.Add("\"" + path + "\"");
                    break;
                case SEARCH.clamscan_folder:
                    param.Add(exclude);
                    param.Add("\"" + path + "\"");
                    break;
                case SEARCH.clamscan_folder_recurse:
                    param.Add("--recursive");
                    param.Add(exclude);
                    param.Add("\"" + path + "\"");
                    break;
                case SEARCH.clamscan_memory:
                    this.Clam.WindowTitle = "Scan memory for viruses";
                    param.Add("--memory");
                    break;
                case SEARCH.clamscan_folderfile:
                    string[] locs = path.Split('|');
                    foreach (string loc in locs)
                    {
                        param.Add("\"" + loc + "\"");
                    }
                    break;
            }

            string full_param = string.Join(" ", param.ToArray());
            full_param = string.Format(full_param,
                Environment.GetEnvironmentVariable("TEMP"),
                this._exec_clam_db_path,
                Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "clamscan_temp_" + Guid.NewGuid().ToString())
            );

            this.log.Clear();
            UpdateProgressLog("╔═══════════════════════════════════════════════╗");
            UpdateProgressLog("║ Starting to scan files and folders for virus. ║");
            UpdateProgressLog("╚═══════════════════════════════════════════════╝");
            UpdateProgressLog("Scanning please wait..");

            if (!launchinbackground)
            {
                ProcessStartInfo startInfo = new ProcessStartInfo(Path.Combine(this._exec_clam, "clamscan.exe"), full_param)
                {
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true,
                };

                update_process = new Process()
                {
                    StartInfo = startInfo
                };
                update_process.OutputDataReceived += process_OutputDataReceived;
                update_process.Start();
                update_process.BeginOutputReadLine();
                update_process.WaitForExit();

                this.Clam.ShowClamWinVirusUpdateWindow = false;
            }
            else
            {
                if (!bgWorker.IsBusy)
                {
                    bgWorker.RunWorkerAsync(Path.Combine(this._exec_clam, "clamscan.exe") + "|" + full_param);
                }
                else
                {
                    MessageBox.Show("ClamAV is currently busy", "mCleaner", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }

            //bgWorker.RunWorkerAsync(full_param);
        }