2017-02-27 8 views
1

J'ai une application C#/WPF que je veux donner un comportement différent selon qu'elle a été démarrée à partir d'un lien épinglé dans la barre des tâches de Windows.Détecte si l'application est épinglée à la barre des tâches

  1. Y a-t-il un moyen de détecter si mon application a été épinglée dans la barre des tâches?
  2. Y at-il un moyen de détecter si mon application a été démarrée à partir d'un élément épinglé dans la barre des tâches?
+0

https://www.codeproject.com/Articles/43768/Windows-Taskbar-Check-if-a-program-or-window-is –

Répondre

3

Vous pouvez détecter si l'application est épinglée sur la barre des tâches pour l'utilisateur actuel en inspectant le dossier %appdata%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar où sont stockés les raccourcis vers toutes les applications épinglées. Par exemple (besoin d'ajouter COM référence à 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; 
} 

Il est également un moyen de détecter si l'application a été lancée à partir d'un élément épinglé ou non. Pour cela vous aurez besoin de GetStartupInfo fonction d'api de victoire. Entre autres informations, il vous fournira un chemin complet vers un raccourci (ou simplement un fichier) où le processus en cours a été démarré. Exemple:

[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; 
} 

Utilisation:

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

Maintenant, si vous avez commencé l'application de la barre des tâches, startupPath pointera vers un raccourci de %appdata%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar, donc avec toutes ces informations, il est facile de vérifier si l'application a été lancée à partir barre des tâches ou non.

+0

Bonne réponse, merci! –