0

Try pin folders to quick access on Win10 with .net6.

I've tried using whether c# or powershell script, they all stuck after InvokeVerb("pintohome"). and there's no any other shell instance or folder open.

Then i have to force quit the shell or just delete it.

There's no further info when i tried using breakpoint in visual studio, just get stuck and nothing happens, no erros and no infos, just nothing.

Here's my code, and basically same as those on whether stackoverflow or msdn.

I just know little about c#, so it's not clear for me to figure out why this happens and how to continue. Has someone have had similar issue?

    public void pin_to_home(string path)
    {
        // If path not exists or is a file, return false
        if (!(File.Exists(path) ^ Directory.Exists(path)))
        {
            Console.WriteLine("Given path is inavlid. " + File.Exists(path));
            return;
        }

        Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
        object pinShell = System.Activator.CreateInstance(shellAppType);
        if (pinShell == null)
        {
            return;
        }
        dynamic pathFolder = shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, pinShell, new object[] { path });
        if (pathFolder == null)
        {
            return;
        }

        Console.WriteLine("Try pin to home: " + path);
        pathFolder.Self.InvokeVerb("pintohome");
    }

or ps script

$Namespace = "shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}"
$QuickAccess = New-Object -ComObject Shell.Application
$RecentFiles = $QuickAccess.Namespace($Namespace).Items()

Write-Output "Start pin to home!"
$QuickAccess.Namespace("D:\TEMP").Self.InvokeVerb("pintohome")
Write-Output "Finish! "
Hellagur
  • 93
  • 7
  • https://devblogs.microsoft.com/oldnewthing/20030903-00/?p=42673 – Anders Dec 01 '22 at 08:57
  • You are creating an instance of cmd.exe and the application is never closing the shell.If you open up a cmd.exe and type "hemp cmd" you will see options /c and /k. The method you are using to create the shell is keeping the shell open after the command completes. It may be better to using Invoke and run cmd.exe and then make you command part of the argument list. See Invoke for powershell. In c# you can use Process class which gives an event when app completes. – jdweng Dec 01 '22 at 11:35
  • @Anders Got it. just try to manage my quick access list using scripts, seems better to just give up and maunally do it... – Hellagur Dec 02 '22 at 01:20
  • @jdweng i believe that's the problem, thx for your help! – Hellagur Dec 02 '22 at 01:21

1 Answers1

0

Still not pretty sure why, but able to 'pin to home' now

Just follow Programatically Pin\UnPin the folder from quick access menu in windows 10

And if you are using .net framework, the reference System.Management.Automation is not available to use if you download it from the nuget

You can search the System.Management.Automation.dll in your computer and copy to your project, manually add reference or check this How to reference System.Management.Automation in a .NET Framework 4.7.2?

Hellagur
  • 93
  • 7