EWUScanner.Program.ScanDirectories C# (CSharp) Method

ScanDirectories() public static method

public static ScanDirectories ( string root ) : void
root string
return void
        public static void ScanDirectories(string root)
        {
            //For now, I will test the passed in root to see if it is excluded from scanning

            if (CheckIfExcluded(root) == false) //root is not excluded
            {//if not excluded, do the scanning

                // Testing code for Michael to scan one directory
                //root = "Y:\\Documents\\College\\11-12\\Spring\\CSCD488\\Testbed";

                int currentSize = 0;

                Stack<string> directories = new Stack<string>(100);

                if (!Directory.Exists(root))
                    throw new ArgumentException();

                directories.Push(root);

                while (directories.Count > 0)
                {
                    string currentDirectory = directories.Pop();

                    if (!currentDirectory.Equals(windowsFolderPath) && !currentDirectory.Equals(programFilesFolderPath))
                    {
                        string[] subDirectories;
                        //get the subDirectories of the current directory
                        try
                        {
                            subDirectories = Directory.GetDirectories(currentDirectory);
                        }
                        catch (UnauthorizedAccessException u)
                        {
                            Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, u.ToString());
                            continue;
                        }
                        catch (System.IO.DirectoryNotFoundException d)
                        {
                            Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, d.ToString());
                            continue;
                        }
                        //I will use a list to store the subDirectories at this point. The string[] is there because Directory.GetDirectories returns a string[], but
                        //I'll need to remove excluded paths. To do that, i'll just copy the included scannable paths to a list.

                        List<string> includedSubDirectories = new List<string>();
                        //populate the includedSubDirectories with the actual paths to be scanned
                        foreach (string directory in subDirectories)
                        {
                            if(CheckIfExcluded(directory) == false) //meaning, the directory IS to be scanned, it is not excluded
                            {
                                includedSubDirectories.Add(directory);
                            }
                        }

                        //at the end of this foreach loop, only the directories to be scanned should be in the includedSubDirectories list
                        string[] files = null;

                        try
                        {
                            files = Directory.GetFiles(currentDirectory);
                        }
                        catch (UnauthorizedAccessException u)
                        {
                            Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, u.ToString());
                            continue;
                        }
                        catch (System.IO.DirectoryNotFoundException d)
                        {
                            Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, d.ToString());
                            continue;
                        }

                        try { mainUIForm.lblCurDir.BeginInvoke(new MainForm.InvokeDelegateFilename(mainUIForm.UpdateLblCurFolder), new object[] { currentDirectory }); }
                        catch (InvalidOperationException) { continue; }

                        //iterate through all the files from the current directory
                        foreach (string file in files)
                        {
                            while (!scanning) { }
                            try
                            {
                                Delimon.Win32.IO.FileInfo fInfo = null;

                                //====Extension Validation: Only create the FileInfo object if it is a targeted file type. ===================================================
                                bool extValid = ValidateExtension(Path.GetExtension(file));
                                if (!extValid)
                                    continue;
                                else
                                    fInfo = new Delimon.Win32.IO.FileInfo(file);
                                //============================================================================================================================================

                                ProcessFile(fInfo);

                                //====Update form fields =====================================================================================================================
                                if (currentSize < mainUIForm.theProgressBar.Maximum)
                                    currentSize += (Convert.ToInt32(fInfo.Length) / 1024);

                                int percentage = (int)((double)currentSize / totalSize * 100);
                                numScanned++;

                                try
                                {

                                    mainUIForm.theProgressBar.BeginInvoke(new MainForm.InvokeDelegateProgressBar(mainUIForm.UpdateProgressBar), new object[] { currentSize });
                                    mainUIForm.lblPercentage.BeginInvoke(new MainForm.InvokeDelegatePercentage(mainUIForm.UpdateLblPercentage), new object[] { percentage });
                                    mainUIForm.lblItemsScanned.BeginInvoke(new MainForm.InvokeDelegateScanned(mainUIForm.UpdateLblItemsScanned), new object[] { numScanned });
                                }
                                catch (InvalidOperationException) { continue; }
                                //============================================================================================================================================

                            }
                            catch (FileNotFoundException f)
                            {
                                Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, f.ToString());
                                continue;
                            }
                            catch (UnauthorizedAccessException u)
                            {
                                Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, u.ToString());
                                continue;
                            }
                        }

                        foreach (string directoryName in includedSubDirectories)
                        {
                            directories.Push(directoryName);
                        }
                    }
                }
            }
        }