Jan Rothkegel
Jan Rothkegel

Reputation: 762

Detect whether application is pinned to taskbar

I have a C#/WPF application which I want to give different behaviour depending on whether it has been started from a pinned link on the windows taskbar.

  1. Is there a way to detect whether my application has been pinned to the taskbar?
  2. Is there a way to detect whether my application has been started from a pinned item on the taskbar?

Upvotes: 4

Views: 1952

Answers (1)

Evk
Evk

Reputation: 101473

You can detect if application is pinned to taskbar for current user by inspecting folder %appdata%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar where shortcuts to all pinned applications are stored. For example (need to add COM reference to Windows Script Host Object Model):

private static bool IsCurrentApplicationPinned() {
    // path to current executable
    var currentPath = Assembly.GetEntryAssembly().Location;            
    // folder with shortcuts
    string location = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar");
    if (!Directory.Exists(location))
        return false;

    foreach (var file in Directory.GetFiles(location, "*.lnk")) {
        IWshShell shell = new WshShell();
        var lnk = shell.CreateShortcut(file) as IWshShortcut;
        if (lnk != null) {  
            // if there is shortcut pointing to current executable - it's pinned                                    
            if (String.Equals(lnk.TargetPath, currentPath, StringComparison.InvariantCultureIgnoreCase)) {
                return true;
            }
        }
    }
    return false;
}

There is also a way to detect if application was started from a pinned item or not. For that you will need GetStartupInfo win api function. Among other info, it will provide you a full path to a shortcut (or just file) current process was started with. Example:

[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetStartupInfoA")]
public static extern void GetStartupInfo(out STARTUPINFO lpStartupInfo);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct STARTUPINFO
{
    public uint cb;
    public string lpReserved;
    public string lpDesktop;
    public string lpTitle;
    public uint dwX;
    public uint dwY;
    public uint dwXSize;
    public uint dwYSize;
    public uint dwXCountChars;
    public uint dwYCountChars;
    public uint dwFillAttribute;
    public uint dwFlags;
    public ushort wShowWindow;
    public ushort cbReserved2;
    public IntPtr lpReserved2;
    public IntPtr hStdInput;
    public IntPtr hStdOutput;
    public IntPtr hStdError;
}

Usage:

STARTUPINFO startInfo;
GetStartupInfo(out startInfo);
var startupPath = startInfo.lpTitle;

Now if you have started application from taskbar, startupPath will point to a shortcut from %appdata%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar, so with all this info it's easy to check if application was started from taskbar or not.

Upvotes: 7

Related Questions