From 1ea9bf531cc1f3733f2365d46893f3f2e49bf6f8 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 10 Dec 2018 10:42:14 +0300 Subject: [PATCH] search function rework --- OWTrack/MainForm.cs | 1 + OWTrack/Tracker.cs | 65 +++++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/OWTrack/MainForm.cs b/OWTrack/MainForm.cs index 9b7f113..3b67ba0 100644 --- a/OWTrack/MainForm.cs +++ b/OWTrack/MainForm.cs @@ -98,6 +98,7 @@ namespace OWTrack { if (!tr.LoacteOW()) { + MessageBox.Show(tr.gamePath); tr.gamePath = getGamePath(); } } diff --git a/OWTrack/Tracker.cs b/OWTrack/Tracker.cs index 8be3fc4..d3c45ed 100644 --- a/OWTrack/Tracker.cs +++ b/OWTrack/Tracker.cs @@ -53,7 +53,6 @@ namespace OWTrack public static readonly string D = "D:\\Program Files"; public static readonly string E = "E:\\Program Files"; public static readonly string F = "F:\\Program Files"; - public static readonly string G = "G:\\Program Files"; } public bool owRunning() @@ -79,43 +78,16 @@ namespace OWTrack { try { - DriveInfo[] Drives = DriveInfo.GetDrives();//Make abstraction, all drives. + DriveInfo[] driveInfo = DriveInfo.GetDrives(); List paths = new List(); - string[] filesC = null; - string[] filesD = null; - - if (ProgramFilesExist('c')) + //Searches all drives (too long) + foreach (var drive in driveInfo) { - foreach ( string file in Directory.GetFiles(ProgramFiles.C, "Overwatch.exe", SearchOption.AllDirectories)) - { - if (file.Contains("Overwatch.exe")) - { - paths.Add(file); - } - } - } - if (ProgramFilesExist('d')) - { - foreach (string file in Directory.GetFiles(ProgramFiles.D, "Overwatch.exe", SearchOption.AllDirectories)) - { - if (file.Contains("Overwatch.exe")) - { - paths.Add(file); - } - } + //paths.AddRange(GetFiles(drive.ToString(),"Overwatch.exe")); } + paths.AddRange(GetFiles(ProgramFiles.C, "Overwatch.exe")); + paths.AddRange(GetFiles(ProgramFiles.D, "Overwatch.exe")); - if (ProgramFilesExist('g')) - { - foreach (string file in Directory.GetFiles(ProgramFiles.G, "Overwatch.exe", SearchOption.AllDirectories)) - { - if (file.Contains("Overwatch.exe")) - { - paths.Add(file); - } - } - } - foreach (var file in paths) { MessageBox.Show(file); } if (paths.Count > 1) { //TODO: ask about correct path @@ -140,8 +112,33 @@ namespace OWTrack { return Directory.Exists(drive+":\\Program Files"); } + + public static IEnumerable GetFiles(string root, string searchPattern) + { + Stack pending = new Stack(); + pending.Push(root); + while (pending.Count != 0) + { + var path = pending.Pop(); + string[] next = null; + try + { + next = Directory.GetFiles(path, searchPattern); + } + catch { } + if (next != null && next.Length != 0) + foreach (var file in next) yield return file; + try + { + next = Directory.GetDirectories(path); + foreach (var subdir in next) pending.Push(subdir); + } + catch { } + } + } } + struct Settings { bool TrackSR, TrackOW;