C #-Pin to Quick Access

1 minute read

Thing you want to do

Quick access is sometimes unpinned. (Maybe a bug in Windows 10)
As a countermeasure, I want to be able to register from the program in one shot.

image.png

The point

Pinning to the specified folder is realized in the following part of the source code described later.


var folder = shell.NameSpace(path);
var item = folder.Self;
item.InvokeVerb("pintohome");

Source code

Pin the folder to quick access by typing the folder (or shortcut to the folder) specified in the command line options.



using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

class SampleProgram
{
    static void PinToQuickAccess(string[] paths)
    {
        if ( paths.Length > 0 ) {
            Type type = Type.GetTypeFromProgID("Shell.Application");
            dynamic shell = Activator.CreateInstance(type);
            foreach ( string tmp in paths ) {
                string path = Path.GetFullPath(tmp);
                if ( File.Exists(path) && path.EndsWith(".lnk",true,null) ) {
                    // lnk file
                    path = GetTargetPath(path) ?? "";
                }
                if ( Directory.Exists(path) ) {
                    var folder = shell.NameSpace(path);
                    var item = folder.Self;
                    item.InvokeVerb("pintohome");
                }
            }
        }

        //COM object release...This should be fine
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    //* Unpinned items are also listed.
    static string[] GetEntriesOfQuickAccess()
    {
        var paths = new List<string>();

        {
            Type type = Type.GetTypeFromProgID("Shell.Application");
            dynamic shell = Activator.CreateInstance(type);
            dynamic folder = shell.NameSpace("shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}");

            if ( folder != null ) {
                foreach (dynamic folderItem in folder.Items()) {
                    string s = folderItem.Path;
                    paths.Add(s);
                    Console.WriteLine(s);
                }
            }
        }

        GC.Collect();
        GC.WaitForPendingFinalizers();

        return paths.ToArray();
    }

    // .Get the path from the lnk file
    static string GetTargetPath(string fullPath)
    {
        {
            Type type = Type.GetTypeFromProgID("WScript.Shell");
            dynamic shell = Activator.CreateInstance(type);// IWshRuntimeLibrary.WshShell
            dynamic lnk = shell.CreateShortcut(fullPath);// IWshRuntimeLibrary.IWshShortcut

            if (string.IsNullOrEmpty(lnk.TargetPath)) {
                return null;
            }

            return lnk.TargetPath;
        }
    }


    [STAThread]
    static void Main(string[] args)
    {
        GetEntriesOfQuickAccess();
        PinToQuickAccess(args);
    }
}

Reference site

-Explorer Quick Access Pinned C # –C # and VB.NET Question Board