2016-06-07 16 views
1

在C#中,獲取.exe文件的圖標很容易。你只是做Icon.ExtractAssociatedIcon(path)和voilà。 但是,如果我想要獲取圖標的應用程序不是標準的.exe文件,而是這些新穎的Windows應用商店應用程序之一呢?如何從C#桌面應用程序中檢索Windows應用商店應用的圖標?

有沒有辦法讓我的C#應用​​程序中的Windows應用商店應用程序的圖標作爲位圖對象?

+0

那麼,你想從第三方應用程序提取圖像?你知道部署後應用程序的存儲位置嗎? – Konstantin

回答

0

這個問題已經回答了關於超級用戶一般方法: https://superuser.com/questions/478975/how-to-create-a-desktop-shortcut-to-a-windows-8-modern-ui-app

如果你想要做的在C# - 這一定程度上取決於你從哪裏開始。你想爲所有應用程序的特定應用程序或圖標的圖標?你知道應用程序,應用程序名稱或應用程序ID的路徑嗎?

您可以使用PowerShell Get-AppxPackage命令獲取應用程序的列表。

using (var runspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault())) 
{ 
    runspace.Open(); 

    PowerShell powerShellCommand = PowerShell.Create(); 
    powerShellCommand.Runspace = runspace; 
    powerShellCommand.AddScript("Get-AppxPackage |?{!$_.IsFramework}"); 

    foreach (var result in powerShellCommand.Invoke()) 
    { 
     try 
     { 
      if (result.Properties.Match("Name").Count > 0 && 
       result.Properties.Match("InstallLocation").Count > 0) 
      { 
       var name = result.Properties["Name"].Value; 
       var installLocation = result.Properties["InstallLocation"].Value; 
       Console.WriteLine(installLocation.ToString()); 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 
    } 

    runspace.Close(); 
} 

上述的問題是,它返回了很多:我還沒有得到它的工作,但要做到這一點從C#,你會安裝​​NuGet包,然後運行這樣的事情太過分了的不是真正的應用程序的組件包。我找不到找到應用顯示名稱的簡單方法。您可以破解安裝位置中找到的AppxManifest.xml文件,但對於任何本地化的應用程序,它的顯示名稱都會顯示「ms-resource:ApplicationTitleWithBranding」,從應用程序外部提取它似乎很複雜。

要從註冊表中獲取應用程序列表,您可以使用Microsoft.Win32.Registry類從「HKEY_CLASSES_ROOT \ Extensions \ ContractId \ Windows.Protocol \ PackageId」中讀取註冊表,然後使用超級用戶所描述的技術獲取該圖塊圖片。

0

在Windows存儲UWP應用程序的情況下,您將擁有所謂「間接字符串」形式的應用程序圖標路徑。 例如對於Groove音樂:

@{Microsoft.ZuneMusic_10.18011.12711.0_x64__8wekyb3d8bbwe?ms-resource://Microsoft.ZuneMusic/Files/Assets/AppList.png}

爲了從間接串SHLoadIndirectString Windows API函數正常路徑都可以使用。

using System; 
using System.Runtime.InteropServices; 
using System.Text; 

class IndirectString 
{ 
    [DllImport("shlwapi.dll", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false, ThrowOnUnmappableChar = true)] 
    public static extern int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, int cchOutBuf, IntPtr ppvReserved); 

    public string ExtractNormalPath(string indirectString) 
    { 
     StringBuilder outBuff = new StringBuilder(1024); 
     int result = SHLoadIndirectString(indirectString, outBuff, outBuff.Capacity, IntPtr.Zero); 

     return outBuff.ToString(); 
    } 
} 
相關問題