summary refs log tree commit diff
path: root/SpacebarDiscordDesktopLauncher/WindowsDiscordClientFinder.cs
diff options
context:
space:
mode:
Diffstat (limited to 'SpacebarDiscordDesktopLauncher/WindowsDiscordClientFinder.cs')
-rw-r--r--SpacebarDiscordDesktopLauncher/WindowsDiscordClientFinder.cs38
1 files changed, 38 insertions, 0 deletions
diff --git a/SpacebarDiscordDesktopLauncher/WindowsDiscordClientFinder.cs b/SpacebarDiscordDesktopLauncher/WindowsDiscordClientFinder.cs
new file mode 100644
index 0000000..8b6db0e
--- /dev/null
+++ b/SpacebarDiscordDesktopLauncher/WindowsDiscordClientFinder.cs
@@ -0,0 +1,38 @@
+namespace SpacebarDiscordDesktopLauncher;
+
+public static class WindowsDiscordClientFinder
+{
+    public static string? FindDiscord()
+    {
+        Console.WriteLine($"Looking for installed Discord desktop instance...");
+
+        var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
+        foreach (var discordBranch in new String[] { "", "PTB", "Canary", "Development" })
+        {
+            if (Path.Exists(Path.Combine(basePath, "Discord" + discordBranch)))
+                return Path.Combine(GetLatestAppVersion(Path.Combine(basePath, "Discord" + discordBranch)),
+                    discordBranch + ".exe");
+        }
+
+        Console.WriteLine("Could not find Discord! Do you have the normal Discord client installed?");
+        Console.ReadLine();
+        return null;
+    }
+
+    private static string GetLatestAppVersion(string basePath)
+    {
+        var dirs = Directory.GetDirectories(basePath);
+        var newestDir = "";
+        var newestDirDate = DateTime.MinValue;
+        foreach (var dir in dirs)
+        {
+            var di = new DirectoryInfo(dir);
+            if (!di.Name.StartsWith("app", StringComparison.InvariantCultureIgnoreCase)) continue;
+            if (di.CreationTimeUtc <= newestDirDate) continue;
+            newestDirDate = di.CreationTimeUtc;
+            newestDir = di.FullName;
+        }
+
+        return newestDir;
+    }
+}
\ No newline at end of file