Skip to content

Commit

Permalink
Allow renaming of taskbar shortcut (#492)
Browse files Browse the repository at this point in the history
Closes #492
  • Loading branch information
srwi committed Apr 7, 2024
1 parent 765408c commit 25347a2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
9 changes: 9 additions & 0 deletions EverythingToolbar.Launcher/EverythingToolbar.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
<COMReference Include="Shell32">
<Guid>{50A7E9B0-70EF-11D1-B75A-00A0C90564FE}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.af.resx" />
Expand Down
57 changes: 40 additions & 17 deletions EverythingToolbar.Launcher/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using EverythingToolbar.Properties;
using IWshRuntimeLibrary;
using Microsoft.Win32;
using Shell32;
using File = System.IO.File;

namespace EverythingToolbar.Launcher
Expand All @@ -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()
Expand All @@ -32,65 +55,65 @@ 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<Utils>().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;
}
}

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();
}
Expand Down

0 comments on commit 25347a2

Please sign in to comment.