gpt4 book ai didi

c# - 如何获取已安装的 Metro 应用程序的显示名称

转载 作者:行者123 更新时间:2023-12-03 19:24:16 30 4
gpt4 key购买 nike

我知道之前有人问过这个问题,我已经查看了 this similar 的答案问题。我试图获取我计算机上安装的所有 Metro 应用程序的显示名称,我想到了这个:

class Program {
[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);

static void Main(string[] args) {
foreach(string dir in Directory.GetDirectories(@"c:\program files\WindowsApps\")) {
if(File.Exists(dir + @"\AppxManifest.xml")) {
XmlDocument doc = new XmlDocument();
doc.Load(dir + @"\AppxManifest.xml");

string name = doc.GetElementsByTagName("DisplayName")[0].InnerText;
string identity = doc.GetElementsByTagName("Identity")[0].Attributes["Name"].Value;

if(!name.Contains("ms-resource")) {
Console.WriteLine(name);
} else {
StringBuilder sb = new StringBuilder();

int result = SHLoadIndirectString(
@"@{" + Path.GetFileName(dir) + "? ms-resource://" + Identity + "/resources/" + name.Split(':')[1] + "}",
sb, -1,
IntPtr.Zero);

if(result == 0) Console.WriteLine(sb.ToString());
}
}
}
Console.ReadLine();
}
}

这很好用,并为我提供了应用程序的名称,直到它到达文件夹 /microsoft.windowscommunicationsapps_17.4.9600.16384_x64__8wekyb3d8bbwe/。 Visual Studio 不提供崩溃报告,它只是说“该程序已停止工作”。我查看了 xml 文件的结构,没有发现它应该崩溃的原因。

所以我的问题是:有没有什么方法可以修复崩溃,或者有没有更好的方法来获取 Metro 应用程序显示名称而不使用任何 Windows 8 特定功能?

谢谢!

最佳答案

我知道我要回答我自己的问题,但我已经做了很多研究并找到了我的问题的答案。这花了一些时间才找到,所以我也将展示我的解决方案并列出我了解到的关于 Metro 应用程序的其他一些事情。

  • Metro 应用安装在 C:/Program Files/WindowsApps 的包(文件夹)中
  • 一个包中可以有多个 Metro 应用
  • 每个包中都有一个 AppxManifest.xml 告诉包中有什么
  • 在 list 中,每个应用程序都有自己的应用程序标签
  • 可以使用 DOS 命令启动 Metro 应用程序:
  • start [ProtocolName]:
  • An App's protocol name can be obtained from the Protocol tag in the AppxManifest.xml

I modified my function to account for two apps being in a single package. I also filtered the list based on whether or not the app has a protocol name.

[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 static List<string> GetMetroAppnames() {
List<string> names = new List<string>();

foreach(string dir in Directory.GetDirectories(@"c:\program files\WindowsApps\")) {
if(System.IO.File.Exists(dir + @"\AppxManifest.xml")) {
XmlDocument doc = new XmlDocument();
doc.Load(dir + @"\AppxManifest.xml");

if(doc.GetElementsByTagName("Framework")[0] != null)
if(doc.GetElementsByTagName("Framework")[0].InnerText.ToLower() == "true")
continue;
if(doc.GetElementsByTagName("Protocol")[0] == null) continue;

string name = doc.GetElementsByTagName("DisplayName")[0].InnerText;
string identity = doc.GetElementsByTagName("Identity")[0].Attributes["Name"].Value;
string appName = "";

if(!name.Contains("ms-resource")) {
names.Add(name);
} else {
if(doc.GetElementsByTagName("Application").Count > 1) {
foreach(XmlElement elem in doc.GetElementsByTagName("Application")) {
name = elem.GetElementsByTagName("m2:VisualElements")[0].Attributes["DisplayName"].Value;
if(name.Contains("AppName")) name = name.Replace("AppName", "AppTitle");
appName = GetName(dir, name, identity);
if(appName != "") names.Add(appName);
}
}
appName = GetName(dir, name, identity);
if(appName != "") names.Add(appName);
}
}
}
return names.Distinct().ToList();
}

private static string GetName(string dir, string name, string identity) {
StringBuilder sb = new StringBuilder();
int result;

result = SHLoadIndirectString(
@"@{" + Path.GetFileName(dir) + "? ms-resource://" + identity + "/resources/" + name.Split(':')[1] + "}",
sb, -1,
IntPtr.Zero
);

if(result == 0) return sb.ToString();
return "";
}

代码似乎有点冗长,但这是我唯一能做到的。如果您稍后再看这个,那么我希望这能帮助您解决问题,但就目前而言,我已经了解了我需要的东西。

关于c# - 如何获取已安装的 Metro 应用程序的显示名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331385/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com