-
Notifications
You must be signed in to change notification settings - Fork 202
/
handleWindowsApp.cs
149 lines (121 loc) · 6 KB
/
handleWindowsApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml;
using System.Collections.Generic;
using Windows.Management.Deployment;
namespace client.Classes
{
class handleWindowsApp
{
public static Dictionary<string, string> fileDirectoryCache = new Dictionary<string, string>();
private static PackageManager pkgManger = new PackageManager();
public static Bitmap getWindowsAppIcon(String file, bool alreadyAppID = false)
{
// Get the app's ID from its shortcut target file (Ex. 4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.app)
String microsoftAppName = (!alreadyAppID) ? GetLnkTarget(file) : file;
// Split the string to get the app name from the beginning (Ex. 4DF9E0F8.Netflix)
String subAppName = microsoftAppName.Split('!')[0];
// Loop through each of the folders with the app name to find the one with the manifest + logos
String appPath = findWindowsAppsFolder(subAppName);
// Load and read manifest to get the logo path
XmlDocument appManifest = new XmlDocument();
appManifest.Load(appPath + "\\AppxManifest.xml");
XmlNamespaceManager appManifestNamespace = new XmlNamespaceManager(new NameTable());
appManifestNamespace.AddNamespace("sm", "http://schemas.microsoft.com/appx/manifest/foundation/windows10");
String logoLocation = (appManifest.SelectSingleNode("/sm:Package/sm:Properties/sm:Logo", appManifestNamespace).InnerText).Replace("\\", @"\");
if (logoLocation != null)
{
// Get the last instance or usage of \ to cut out the path of the logo just to have the path leading to the general logo folder
logoLocation = logoLocation.Substring(0, logoLocation.LastIndexOf(@"\"));
String logoLocationFullPath = Path.GetFullPath(appPath + "\\" + logoLocation);
// Search for all files with 150x150 in its name and use the first result
DirectoryInfo logoDirectory = new DirectoryInfo(logoLocationFullPath);
FileInfo[] filesInDir = getLogoFolder("StoreLogo", logoDirectory);
if (filesInDir.Length != 0)
{
return getLogo(filesInDir.Last().FullName, file);
}
else
{
filesInDir = getLogoFolder("scale-200", logoDirectory);
if (filesInDir.Length != 0)
{
return getLogo(filesInDir[0].FullName, file);
} else
{
return Icon.ExtractAssociatedIcon(file).ToBitmap();
}
}
} else
{
return Icon.ExtractAssociatedIcon(file).ToBitmap();
}
}
private static FileInfo[] getLogoFolder(String keyname, DirectoryInfo logoDirectory)
{
// Search for all files with the keyname in its name and use the first result
FileInfo[] filesInDir = logoDirectory.GetFiles("*" + keyname + "*.*");
return filesInDir;
}
private static Bitmap getLogo(String logoPath, String defaultFile)
{
if (File.Exists(logoPath))
{
using (MemoryStream ms = new MemoryStream(System.IO.File.ReadAllBytes(logoPath)))
return ImageFunctions.ResizeImage(Bitmap.FromStream(ms), 64, 64);
}
else
{
return Icon.ExtractAssociatedIcon(defaultFile).ToBitmap();
}
}
public static string GetLnkTarget(string lnkPath)
{
var shl = new Shell32.Shell();
lnkPath = System.IO.Path.GetFullPath(lnkPath);
var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath));
var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath));
var lnk = (Shell32.ShellLinkObject)itm.GetLink;
return lnk.Target.Path;
}
public static string findWindowsAppsFolder(string subAppName)
{
if (!fileDirectoryCache.ContainsKey(subAppName))
{
try
{
IEnumerable<Windows.ApplicationModel.Package> packages = pkgManger.FindPackagesForUser("", subAppName);
String finalPath = Environment.ExpandEnvironmentVariables("%ProgramW6432%") + $@"\WindowsApps\" + packages.First().InstalledLocation.DisplayName + @"\";
fileDirectoryCache[subAppName] = finalPath;
return finalPath;
}
catch (UnauthorizedAccessException) { };
return "";
}
else
{
return fileDirectoryCache[subAppName];
}
}
public static string findWindowsAppsName(string AppName)
{
String subAppName = AppName.Split('!')[0];
String appPath = findWindowsAppsFolder(subAppName);
// Load and read manifest to get the logo path
XmlDocument appManifest = new XmlDocument();
appManifest.Load(appPath + "\\AppxManifest.xml");
XmlNamespaceManager appManifestNamespace = new XmlNamespaceManager(new NameTable());
appManifestNamespace.AddNamespace("sm", "http://schemas.microsoft.com/appx/manifest/foundation/windows10");
appManifestNamespace.AddNamespace("uap", "http://schemas.microsoft.com/appx/manifest/uap/windows10");
try
{
return appManifest.SelectSingleNode("/sm:Package/sm:Applications/sm:Application/uap:VisualElements", appManifestNamespace).Attributes.GetNamedItem("DisplayName").InnerText;
} catch (Exception)
{
return appManifest.SelectSingleNode("/sm:Package/sm:Properties/sm:DisplayName", appManifestNamespace).InnerText;
}
}
}
}