From 08215a8a776948f246686850d3a66a56cc2f10f6 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Thu, 22 Jun 2023 14:28:13 +0200 Subject: [PATCH] Cleanup and NullReferenceException fix (#26956) --- .../VSCodeHelper/VSCodeInstances.cs | 128 +++++++++--------- 1 file changed, 61 insertions(+), 67 deletions(-) diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/VSCodeHelper/VSCodeInstances.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/VSCodeHelper/VSCodeInstances.cs index 111b6b833b..98b2d69169 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/VSCodeHelper/VSCodeInstances.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/VSCodeHelper/VSCodeInstances.cs @@ -15,9 +15,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper { public static class VSCodeInstances { - private static List _paths = new List(); - - private static string _userAppDataPath = Environment.GetEnvironmentVariable("AppData"); + private static readonly string _userAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); public static List Instances { get; set; } = new List(); @@ -44,7 +42,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper int bitmap1Width = bitmap1.Width; int bitmap1Height = bitmap1.Height; - Bitmap overlayBitmapResized = new Bitmap(overlayBitmap, new System.Drawing.Size(bitmap1Width / 2, bitmap1Height / 2)); + using Bitmap overlayBitmapResized = new Bitmap(overlayBitmap, new System.Drawing.Size(bitmap1Width / 2, bitmap1Height / 2)); float marginLeft = (float)((bitmap1Width * 0.7) - (overlayBitmapResized.Width * 0.5)); float marginTop = (float)((bitmap1Height * 0.7) - (overlayBitmapResized.Height * 0.5)); @@ -62,84 +60,80 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper // Gets the executablePath and AppData foreach instance of VSCode public static void LoadVSCodeInstances() { - var environmentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); - environmentPath += (environmentPath.Length > 0 && environmentPath.EndsWith(';') ? ";" : string.Empty) + Environment.GetEnvironmentVariable("PATH"); - var paths = environmentPath.Split(";").ToList(); - paths = paths.Distinct().ToList(); + var environmentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User) ?? string.Empty; + environmentPath += (environmentPath.Length > 0 && environmentPath.EndsWith(';') ? string.Empty : ";") + Environment.GetEnvironmentVariable("PATH") ?? string.Empty; + var paths = environmentPath + .Split(';') + .Distinct() + .Where(x => x.Contains("VS Code", StringComparison.OrdinalIgnoreCase) + || x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase) + || x.Contains("vscode", StringComparison.OrdinalIgnoreCase)).ToArray(); - var deletedItems = paths.Except(_paths).Any(); - var newItems = _paths.Except(paths).Any(); - - if (newItems || deletedItems) + foreach (var path in paths) { - Instances = new List(); - - paths = paths.Where(x => - x.Contains("VS Code", StringComparison.OrdinalIgnoreCase) || - x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase) || - x.Contains("vscode", StringComparison.OrdinalIgnoreCase)).ToList(); - foreach (var path in paths) + if (Directory.Exists(path)) { - if (Directory.Exists(path)) + var files = Directory.GetFiles(path) + .Where(x => (x.Contains("code", StringComparison.OrdinalIgnoreCase) || x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase)) + && !x.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)).ToArray(); + + var iconPath = Path.GetDirectoryName(path); + + if (files.Length > 0) { - var files = Directory.GetFiles(path); - var iconPath = Path.GetDirectoryName(path); - files = files.Where(x => - (x.Contains("code", StringComparison.OrdinalIgnoreCase) || - x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase)) - && !x.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)).ToArray(); + var file = files[0]; + var version = string.Empty; - if (files.Length > 0) + var instance = new VSCodeInstance { - var file = files[0]; - var version = string.Empty; + ExecutablePath = file, + }; - var instance = new VSCodeInstance - { - ExecutablePath = file, - }; + if (file.EndsWith("code", StringComparison.OrdinalIgnoreCase)) + { + version = "Code"; + instance.VSCodeVersion = VSCodeVersion.Stable; + } + else if (file.EndsWith("code-insiders", StringComparison.OrdinalIgnoreCase)) + { + version = "Code - Insiders"; + instance.VSCodeVersion = VSCodeVersion.Insiders; + } + else if (file.EndsWith("code-exploration", StringComparison.OrdinalIgnoreCase)) + { + version = "Code - Exploration"; + instance.VSCodeVersion = VSCodeVersion.Exploration; + } + else if (file.EndsWith("VSCodium", StringComparison.OrdinalIgnoreCase)) + { + version = "VSCodium"; + instance.VSCodeVersion = VSCodeVersion.Stable; // ? + } - if (file.EndsWith("code", StringComparison.OrdinalIgnoreCase)) - { - version = "Code"; - instance.VSCodeVersion = VSCodeVersion.Stable; - } - else if (file.EndsWith("code-insiders", StringComparison.OrdinalIgnoreCase)) - { - version = "Code - Insiders"; - instance.VSCodeVersion = VSCodeVersion.Insiders; - } - else if (file.EndsWith("code-exploration", StringComparison.OrdinalIgnoreCase)) - { - version = "Code - Exploration"; - instance.VSCodeVersion = VSCodeVersion.Exploration; - } - else if (file.EndsWith("VSCodium", StringComparison.OrdinalIgnoreCase)) - { - version = "VSCodium"; - instance.VSCodeVersion = VSCodeVersion.Stable; // ? - } + if (version != string.Empty) + { + var portableData = Path.Join(iconPath, "data"); + instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version); - if (version != string.Empty) + var vsCodeIconPath = Path.Join(iconPath, $"{version}.exe"); + var vsCodeIcon = Icon.ExtractAssociatedIcon(vsCodeIconPath); + + if (vsCodeIcon != null) { - var portableData = Path.Join(iconPath, "data"); - instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version); - - var iconVSCode = Path.Join(iconPath, $"{version}.exe"); - - var bitmapIconVscode = Icon.ExtractAssociatedIcon(iconVSCode).ToBitmap(); + using var vsCodeIconBitmap = vsCodeIcon.ToBitmap(); // workspace - var folderIcon = (Bitmap)System.Drawing.Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//folder.png"); - instance.WorkspaceIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(folderIcon, bitmapIconVscode)); + using var folderIcon = (Bitmap)Image.FromFile(Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images//folder.png")); + using var bitmapFolderIcon = BitmapOverlayToCenter(folderIcon, vsCodeIconBitmap); + instance.WorkspaceIconBitMap = Bitmap2BitmapImage(bitmapFolderIcon); // remote - var monitorIcon = (Bitmap)System.Drawing.Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//monitor.png"); - - instance.RemoteIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(monitorIcon, bitmapIconVscode)); - - Instances.Add(instance); + using var monitorIcon = (Bitmap)Image.FromFile(Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images//monitor.png")); + using var bitmapMonitorIcon = BitmapOverlayToCenter(monitorIcon, vsCodeIconBitmap); + instance.RemoteIconBitMap = Bitmap2BitmapImage(bitmapMonitorIcon); } + + Instances.Add(instance); } } }