EWUScanner.Program.RunScan C# (CSharp) Method

RunScan() public static method

public static RunScan ( bool adminMode, List paths ) : void
adminMode bool
paths List
return void
        public static void RunScan(bool adminMode, List<string> paths)
        {
            //getting the exclusion paths from the MainForm. The list should be guaranteed to be only strings.
            exclusionPaths = paths;

            if (adminMode) //Admin Mode
            {
                //List of all the drives on the system.
                DriveInfo[] listOfDrives = DriveInfo.GetDrives();

                //check for drive exclusions in the exclusionPaths string
                foreach (string path in exclusionPaths)
                {//traverse through all the exclusionPaths
                    for (int i = 0; i < listOfDrives.Length; i++)
                    {
                        try
                        {
                            if (listOfDrives[i].Name.StartsWith(path))
                            {
                                listOfDrives[i] = null; //for now, i'll set that index to null. I'm uncomfortable setting it to "", I worry that the logic checking might let that drive name "" slip through somehow. But a NULL is pretty blatant.
                                //I'll keep the NULL and implement the error-checking code.
                            }
                        }
                        catch (NullReferenceException)
                        {
                            continue; //i'm setting certain indices to null, and rescanning all indices everytime. Continue will help the scans keep going.
                        }
                    }
                }

                //====Alerts the user of all targeted drives===============================================================================================
                string drives = "";
                foreach (DriveInfo drive in listOfDrives)
                {
                    try
                    {
                        if ((drive.DriveType == DriveType.Removable && drive.IsReady) || drive.DriveType == DriveType.Fixed)
                            drives += drive.Name + " ";
                    }
                    catch (NullReferenceException)
                    {//Due to my exclusion path checking, certain drives may be excluded and set as null due to my code.
                        //This is to catch that error.
                        continue; //continue iterating through the listOfDrives
                    }
                }

                if (!String.IsNullOrEmpty(drives))
                {
                    MessageBox.Show("The following drive(s) have been detected and will be scanned: " + drives +
                                    "\nIf you do not wish to scan these drives please remove them now as doing so during the scan is inadvisable.");
                }
                //==========================================================================================================================================

                //====initial pass to count how many files are on each drive.================================================================================
                foreach (DriveInfo drive in listOfDrives)
                {
                    try
                    {
                        if ((drive.DriveType == DriveType.Removable && drive.IsReady) || drive.DriveType == DriveType.Fixed)
                            totalSize += DetermineSize(drive.Name);
                    }
                    catch (NullReferenceException)
                    {//again, due to my coding, there may be a drive missing.
                        continue;
                    }
                }
                //===========================================================================================================================================

                //====Update form fields: Set the Maximum of the progress bar. Close the initializing form.==================================================
                try
                {
                    mainUIForm.theProgressBar.BeginInvoke(new MainForm.InvokeDelegateProgressBarMax(mainUIForm.SetProgressBarMax), new object[] { totalSize });
                    mainUIForm.BeginInvoke(new MainForm.InvokeDelegateInitForm(mainUIForm.InitFormVisible), new object[] { false });
                }
                catch (InvalidOperationException) { }
                //===========================================================================================================================================

                //====Second pass that parses and scans targeted files.======================================================================================
                foreach (DriveInfo drive in listOfDrives)
                {
                    try
                    {

                        if ((drive.DriveType == DriveType.Removable && drive.IsReady) || drive.DriveType == DriveType.Fixed)
                            ScanDirectories(drive.Name);
                    }
                    catch (NullReferenceException)
                    {//skip the null drive and move on
                        continue;
                    }
                }
                //============================================================================================================================================
            }
            else //Basic Mode
            {
                //initially, check if usersDirectory was excluded from the scan
                if (CheckIfExcluded(usersDirectory) == false) //CheckIfExcluded returns true for excluded and false if the file is included in the scan.
                {
                    totalSize = DetermineSize(usersDirectory);
                }
                else
                {
                    totalSize = 0; //In this basic scan mode, the usersDirectory is the only thing being scanned. If for some reason the usersDirectory is excluded
                    //from the scan and basic scan mode is run..then there will be no paths to scan, so the totalSize of the files will be 0.
                }
                try
                {
                    mainUIForm.theProgressBar.BeginInvoke(new MainForm.InvokeDelegateProgressBarMax(mainUIForm.SetProgressBarMax), new object[] { totalSize });
                    mainUIForm.BeginInvoke(new MainForm.InvokeDelegateInitForm(mainUIForm.InitFormVisible), new object[] { false });
                }
                catch (InvalidOperationException) { }

                //ScanDirectories will have exclusion path checking, hopefully that will be more modularized.
                ScanDirectories(usersDirectory);
            }

            //Update the form to indicate the scan is finished.
            try { mainUIForm.BeginInvoke(new MainForm.InvokeDelegateScanFinished(mainUIForm.UpdateScanFinished), new object[] { }); }
            catch (InvalidOperationException) { }
        }