Get all WinRT application from regular Desktop application. Part 2.

Let’s continue with WinRT APIs. On the previous post I described how to get all WinRT application. Now I’m going to get some data from these applications.

Assume after the first part we have a Package object. What I would like to get are application logos or application background color if logos don’t exist. The windows store enforces all Modern UI applications to have a logo-path in their manifest file. So whole task is actually getting a manifest file and then parse them.

The following code sample describes this flow:

namespace MetroUILogo
{
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;

    using Windows.ApplicationModel;

    public class MainfestInfo
    {
        public MainfestInfo(string logoPath, string smallLogoPath, string backgroundColor)
        {
            LogoPath = logoPath;
            SmallLogoPath = smallLogoPath;
            BackgroundColor = backgroundColor;
        }

        public string BackgroundColor { get; private set; }

        public string LogoPath { get; private set; }

        public string SmallLogoPath { get; private set; }
    }

    public class WinRTManifestLogoExtracter
    {
        public static MainfestInfo Extract(Package package)
        {
            if (package == null || package.InstalledLocation == null
                || string.IsNullOrEmpty(package.InstalledLocation.Path))
            {
                return null;
            }

            var manifestPath =
                Directory.GetFiles(package.InstalledLocation.Path).FirstOrDefault(it => it.EndsWith("AppxManifest.xml"));

            if (string.IsNullOrEmpty(manifestPath))
            {
                return null;
            }

            var xDoc = XDocument.Load(manifestPath);
            if (xDoc.Root == null)
            {
                return null;
            }

            var applicationsElement = xDoc.Root.Elements().FirstOrDefault(it => it.Name.LocalName == "Applications");
            if (applicationsElement == null)
            {
                return null;
            }

            var applicationElement =
                applicationsElement.Elements().FirstOrDefault(it => it.Name.LocalName == "Application");
            if (applicationElement == null)
            {
                return null;
            }

            var visualElement = applicationElement.Elements()
                                                  .FirstOrDefault(it => it.Name.LocalName == "VisualElements");
            if (visualElement == null)
            {
                return null;
            }

            var logo = visualElement.Attribute("Logo").Value;
            var smallLogo = visualElement.Attribute("SmallLogo").Value;
            var backgroundColor = visualElement.Attribute("BackgroundColor").Value;

            var logoPath = GetLogoPath(package.InstalledLocation.Path, logo);
            var smallLogoPath = GetLogoPath(package.InstalledLocation.Path, smallLogo);

            return new MainfestInfo(logoPath, smallLogoPath, backgroundColor);
        }

        private static string GetLogoPath(string directory, string logo)
        {
            if (string.IsNullOrEmpty(logo))
                return string.Empty;

            // original file
            var logoPath = Path.Combine(directory, logo);
            if (File.Exists(logoPath))
                return logoPath;

            // scale_100
            var fileName = Path.GetFileName(logo);
            var extension = Path.GetExtension(logo);
            if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(extension))
                return string.Empty;

            var scaleFileName = string.Format("{0}.scale-100{1}", fileName.Replace(extension, string.Empty), extension);
            logoPath = Path.Combine(directory, logo.Replace(fileName, scaleFileName));
            if (File.Exists(logoPath))
                return logoPath;

            return logo;
        }
    }
}

The demo project you can download from my Bitbucket.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s