Open fgimian opened 1 year ago
P.S.: My ultimate goal here is to update my Quick Access paths if they differ from the desired set of paths.
Something like this:
using Windows.Win32.UI.Shell;
var paths = new[]
{
@"C:\ProgramData",
@"C:\Users\Fots",
@"C:\Users\Fots\AppData\Local",
@"C:\Users\Fots\AppData\Roaming",
@"C:\Users\Fots\Documents",
@"C:\Users\Fots\Downloads",
@"C:\Users\Fots\Music",
@"C:\Users\Fots\Pictures",
@"C:\Users\Fots\Source",
@"C:\Users\Fots\Videos",
};
Shell shell = new();
IShellDispatch shellDispatch = (IShellDispatch)shell;
var qaFolder = shellDispatch.NameSpace("shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}");
var qaFolderItems = qaFolder.Items();
var pinnedPaths = new List<string>();
for (var i = 0; i < qaFolderItems.Count; i++)
{
var qaFolderItem = qaFolderItems.Item(i);
if (qaFolderItem.IsFolder == 0)
{
continue;
}
var qaFolderItemVerbs = qaFolderItem.Verbs();
var pinned = false;
for (int j = 0; j < qaFolderItemVerbs.Count; j++)
{
var qaFolderItemVerb = qaFolderItemVerbs.Item(j);
if (qaFolderItemVerb.Name.ToString() == "Unpin from Quick access")
{
pinned = true;
break;
}
}
if (pinned)
{
pinnedPaths.Add(qaFolderItem.Path.ToString());
}
}
if (!pinnedPaths.SequenceEqual(paths))
{
for (var i = 0; i < qaFolderItems.Count; i++)
{
var qaFolderItem = qaFolderItems.Item(i);
qaFolderItem.InvokeVerb("unpinfromhome");
}
foreach (var path in paths)
{
var folder = shellDispatch.NameSpace(path);
var folder2 = (Folder2)folder;
folder2.Self.InvokeVerb("pintohome");
}
}
Actual behavior
In attempting to obtain a list of Quick Access folder paths using
Shell
andIShellDispatch
, accessing anyFolderItem
properties results in an exception.Expected behavior
I've implemented similar logic using windows-rs and in Go too and in both cases, the properties return the appropriate values correctly.
Repro steps
NativeMethods.txt
content:NativeMethods.json
content (if present): N/AAny of your own code that should be shared?
Exception output is as follows:
Context
LangVersion
(if explicitly set by project): N/AThanks heaps Fotis