簡體   English   中英

如何使用c#從shell:AppsFolder虛擬文件夾中檢索項目及其詳細信息?

[英]How can I retrieve items and their details from the shell:AppsFolder virtual folder using c#?

我正在嘗試從FOLDERID_AppsFolder中獲取所有項目,您可以通過運行explorer.exe shell:appsFolder命令對其進行訪問,及其詳細信息,尤其是AppUserModelID。

我可以使用下面的代碼獲取商品的名稱,但是我不確定如何獲取AppUserModelID。 我能以某種方式獲得此價值嗎?

IShellItem appsFolder;
string str;
var res = ShellItemUtilities.SHGetKnownFolderItem(ShellItemUtilities.FOLDERID_AppsFolder,
              0, IntPtr.Zero, typeof(IShellItem).GUID, out appsFolder);
if (res < 0) return;

try
{
    var pidl = default(PIDLIST_ABSOLUTE);
    foreach (var app in appsFolder.Enumerate())
    {                    
        try
        {
            recyleBin.GetDisplayName(2, out ptr);
            // Get the actual name of the item
            str = Marshal.PtrToStringUni(ptr);
        }
        finally
        {   
            if (ptr != IntPtr.Zero)
            {
                Marshal.FreeCoTaskMem(ptr);
                ptr = IntPtr.Zero;
            }
        }
    }
}
...

也許IShellItem::GetAttributes方法是我所需要的,但它只能檢索我通過sfgaoMask參數指定的屬性,有關此參數值的文檔不包括與AppUserModelID相關的任何內容。

供參考,這是apps文件夾的外觀: 在此處輸入圖片說明

你能聽到the的聲音嗎?

當我偶然發現Microsoft.WindowsAPICodePack-Shell nuget程序包時,我最終找到了解決此問題的方法,該程序包包裝了我所需的所有shell命令,因此我不必P /調用它們。 現在的代碼變為:

// GUID taken from https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid
var FODLERID_AppsFolder = new Guid("{1e87508d-89c2-42f0-8a7e-645a0f50ca58}");
ShellObject appsFolder = (ShellObject)KnownFolderHelper.FromKnownFolderId(FODLERID_AppsFolder);

foreach (var app in (IKnownFolder)appsFolder)
{
    string name = app.Name;
    // The ParsingName property is the AppUserModelID
    string appUserModelID = app.ParsingName; // or app.Properties.System.AppUserModel.ID
    ImageSource icon =  app.Thumbnail.MediumBitmapSource;
}

ShellObject對象實際上包含更多可通過其Properties.System屬性獲得的屬性。

如果您想知道為什么我在實例化時將appsFolder轉換為ShellObjectIKnownFolder在枚舉時僅將其轉換回IKnownFolder ,這是因為API代碼包實際上帶有ShellObjectWatcher ,該對象接受ShellObject並監視其變化。 如果安裝了新應用程序,並且該應用程序在此虛擬文件夾中列出,則可以使用觀察程序對此進行監視:

ShellObjectWatcher sow = new ShellObjectWatcher(appFolder, false);
sow.AllEvents += (s, e) => DoWhatever();
sow.Start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM