From 25347a274e7fb8751d97c32aae0ee7f5a5f2e7f5 Mon Sep 17 00:00:00 2001 From: Stephan Rumswinkel <17520641+srwi@users.noreply.github.com> Date: Sun, 7 Apr 2024 21:17:49 +0200 Subject: [PATCH] Allow renaming of taskbar shortcut (#492) Closes #492 --- .../EverythingToolbar.Launcher.csproj | 9 +++ EverythingToolbar.Launcher/Utils.cs | 57 +++++++++++++------ 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/EverythingToolbar.Launcher/EverythingToolbar.Launcher.csproj b/EverythingToolbar.Launcher/EverythingToolbar.Launcher.csproj index 22e2bac63..34caabb44 100644 --- a/EverythingToolbar.Launcher/EverythingToolbar.Launcher.csproj +++ b/EverythingToolbar.Launcher/EverythingToolbar.Launcher.csproj @@ -114,6 +114,15 @@ False True + + {50A7E9B0-70EF-11D1-B75A-00A0C90564FE} + 1 + 0 + 0 + tlbimp + False + True + diff --git a/EverythingToolbar.Launcher/Utils.cs b/EverythingToolbar.Launcher/Utils.cs index aba210d4f..2a711cd55 100644 --- a/EverythingToolbar.Launcher/Utils.cs +++ b/EverythingToolbar.Launcher/Utils.cs @@ -5,6 +5,7 @@ using EverythingToolbar.Properties; using IWshRuntimeLibrary; using Microsoft.Win32; +using Shell32; using File = System.IO.File; namespace EverythingToolbar.Launcher @@ -20,8 +21,30 @@ public static class WindowsVersion public static string GetTaskbarShortcutPath() { - const string relativePath = @"Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\EverythingToolbar.lnk"; - return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), relativePath); + const string relativeTaskBarPath = @"Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"; + var taskBarPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), relativeTaskBarPath); + + if (Directory.Exists(taskBarPath)) + { + var lnkFiles = Directory.GetFiles(taskBarPath, "*.lnk"); + var shell = new Shell(); + var thisExecutableName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName); + foreach (var lnkFile in lnkFiles) + { + var folder = shell.NameSpace(Path.GetDirectoryName(lnkFile)); + var folderItem = folder.ParseName(Path.GetFileName(lnkFile)); + if (folderItem != null && folderItem.IsLink) + { + var link = (ShellLinkObject)folderItem.GetLink; + var linkFileName = Path.GetFileName(link.Path); + + if (linkFileName == thisExecutableName) + return lnkFile; + } + } + } + + return Path.Combine(taskBarPath, "EverythingToolbar.lnk"); } public static bool IsTaskbarCenterAligned() @@ -32,35 +55,35 @@ public static bool IsTaskbarCenterAligned() if (Helpers.Utils.GetWindowsVersion() < Helpers.Utils.WindowsVersion.Windows11) return false; - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced")) + using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced")) { - object taskbarAlignment = key?.GetValue("TaskbarAl"); + var taskbarAlignment = key?.GetValue("TaskbarAl"); ToolbarLogger.GetLogger().Debug($"taskbarAlignment: {taskbarAlignment}"); - bool leftAligned = taskbarAlignment != null && (int)taskbarAlignment == 0; + var leftAligned = taskbarAlignment != null && (int)taskbarAlignment == 0; return !leftAligned; } } public static bool GetWindowsSearchEnabledState() { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Search")) + using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Search")) { - object searchboxTaskbarMode = key?.GetValue("SearchboxTaskbarMode"); + var searchboxTaskbarMode = key?.GetValue("SearchboxTaskbarMode"); return searchboxTaskbarMode != null && (int)searchboxTaskbarMode > 0; } } public static void SetWindowsSearchEnabledState(bool enabled) { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Search", true)) + using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Search", true)) { - key.SetValue("SearchboxTaskbarMode", enabled ? 1 : 0); + key?.SetValue("SearchboxTaskbarMode", enabled ? 1 : 0); } } public static bool GetAutostartState() { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run")) + using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run")) { return key?.GetValue("EverythingToolbar") != null; } @@ -68,29 +91,29 @@ public static bool GetAutostartState() public static void SetAutostartState(bool enabled) { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true)) + using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true)) { if (enabled) - key.SetValue("EverythingToolbar", "\"" + Process.GetCurrentProcess().MainModule.FileName + "\""); + key?.SetValue("EverythingToolbar", "\"" + Process.GetCurrentProcess().MainModule.FileName + "\""); else - key.DeleteValue("EverythingToolbar", false); + key?.DeleteValue("EverythingToolbar", false); } } public static void ChangeTaskbarPinIcon(string iconName) { - string taskbarShortcutPath = GetTaskbarShortcutPath(); + var taskbarShortcutPath = GetTaskbarShortcutPath(); if (File.Exists(taskbarShortcutPath)) File.Delete(taskbarShortcutPath); - WshShell shell = new WshShell(); - IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(taskbarShortcutPath); + var shell = new WshShell(); + var shortcut = (IWshShortcut)shell.CreateShortcut(taskbarShortcutPath); shortcut.TargetPath = Process.GetCurrentProcess().MainModule.FileName; shortcut.IconLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, iconName); shortcut.Save(); - foreach (Process process in Process.GetProcessesByName("explorer")) + foreach (var process in Process.GetProcessesByName("explorer")) { process.Kill(); }