private void ScanDir(string path) { // uses the file scan to scan all files in a directory and its sub-directories List <string> viruses = new List <string>(); foreach (string file in Directory.GetFiles(path, "*.pdf", SearchOption.AllDirectories)) { if (ScanFile(file)) { viruses.Add(file); } } if (viruses.Count == 0) { MessageBox.Show("All clear!"); } else { MessageBox.Show(String.Format("{0} viruses found!", viruses.Count)); foreach (string virus in viruses) { if (SettingsBox.GetItemChecked(NOTIFY_ID)) { MessageBox.Show(virus); } } } }
private bool ScanFile(string path) {// operation for scanning a file if (Path.GetExtension(path) == ".pdf") { try { PdfDocument file = PdfReader.Open(path); PdfReference nextReference = (PdfReference)file.Internals.Catalog.Elements["/OpenAction"]; if (nextReference == null) { return(false); } PdfDictionary action = (PdfDictionary)nextReference.Value; if (action == null) { return(false); } do { foreach (string pattern in openActionBlacklist) { if (SettingsBox.GetItemChecked(SUSPICIOUS_ID) || Regex.Match(action.ToString(), pattern).Success) { if (SettingsBox.GetItemChecked(REMOVE_ID)) { File.Delete(path); } return(true); } } } // a joke line which basicly iterates over the linked list of open actions while ((nextReference = (PdfReference)action.Elements["/Next"]) == null || (action = (PdfDictionary)nextReference.Value) == null); } catch (UnauthorizedAccessException) { MessageBox.Show(String.Format("couldn't handle {0}. Try to rerun as administrator.", path)); } catch (Exception) { MessageBox.Show(String.Format("{0} is corrupted. Readers may handle it but it prevents us from scanning it fully.", path)); } } return(false); }