Search code examples
c#.nettaskbar

Pin Application to Taskbar using C#


I have searched for this same topic and I found the solution. But the problem is the NameSpace property is giving exception. I have tried with this code :

    public bool PinUnpinTaskbar(string filePath, bool pin)
    {
       
       if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);
        int MAX_PATH = 255;
        var actionIndex = pin ? 5386 : 5387; // 5386 is the DLL index for"Pin to Tas&kbar", ref. http://www.win7dll.info/shell32_dll.html
                                             //uncomment the following line to pin to start instead
                                             //actionIndex = pin ? 51201 : 51394;
        StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
        IntPtr hShell32 = LoadLibrary("Shell32.dll");
        LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
        
        string localizedVerb = szPinToStartLocalized.ToString();

        string path = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(filePath);

        // create the shell application object
        //var shell = new Shell32.Shell();
        dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
        dynamic directory = shellApplication.NameSpace(path);
        dynamic link = directory.ParseName(fileName);

        dynamic verbs = link.Verbs();
        for (int i = 0; i < verbs.Count(); i++)
        {
            dynamic verb = verbs.Item(i);
            if (verb.Name.Equals(localizedVerb))
            {
                verb.DoIt();
                return true;
            }
        }
        return false;
    }

But the problem is with shellApplication.Namespace(path) here. It can not find such property/Method. I don't have too much idea on this. So, any kind of help will be appreciated.


Solution

  • WRT W10: The only way I have ever been able to make any sense of this is;

    It's up to user to "pin to taskbar". However, you must setup a reference for when it eventually happens. That is, if you want your app to combine the app and quick launch icons. Otherwise, a second icon will be created in the taskbar every time the program starts.

    public void MakeAppPinnable()
    {
        var TaskbarPath =
            Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
            "\\AppData\\Roaming\\Microsoft\\Internet Explorer\\Quick Launch";
    
        CreateShortcut(
            System.IO.Path.Combine(TaskbarPath, <AppName>+ ".lnk"),
            System.Reflection.Assembly.GetExecutingAssembly().Location
            );
     }
    
     private void CreateShortcut(string LnkPath, string ExePath)
     {
         if (System.IO.File.Exists(LnkPath)) {return;}
    
         var shell = new IWshRuntimeLibrary.WshShell();
         var shortcut = (IWshRuntimeLibrary.IWshShortcut)(shell.CreateShortcut(LnkPath));
         shortcut.TargetPath = shortcut.IconLocation = ExePath;
         shortcut.Save();
      }
    

    When the user manually pins app to taskbar, a second shortcut is created in the quick launch folder. However, this one lands in ...\Quick Launch\User Pinned\TaskBar. Do not ask me why this all needs to happen in ...\Roaming\Microsoft\Internet Explorer... MADNESS!

    Cheers.