Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the AppxMetadata class for an appx package.
        /// </summary>
        /// <param name="filePath">the path to the appx file</param>
        public AppxMetadata(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(string.Format("{0} not found", filePath));
            }

            this.FilePath = filePath;
            IStream      packageStream  = StreamUtils.CreateInputStreamOnFile(this.FilePath);
            IAppxFactory packageFactory = (IAppxFactory) new AppxFactory();

            this.AppxReader = packageFactory.CreatePackageReader(packageStream);

            this.BlockmapStream = this.AppxReader.GetBlockMap().GetStream();

            IAppxManifestReader appxManifestReader = this.AppxReader.GetManifest();

            this.ManifestStream = appxManifestReader.GetStream();
            IAppxManifestPackageId packageId = appxManifestReader.GetPackageId();

            this.PackageName       = packageId.GetName();
            this.PackageFamilyName = packageId.GetPackageFamilyName();
            this.PackageFullName   = packageId.GetPackageFullName();
            this.Publisher         = packageId.GetPublisher();
            this.Version           = new VersionInfo(packageId.GetVersion());

            IAppxManifestProperties packageProperties = appxManifestReader.GetProperties();
            string displayName;

            packageProperties.GetStringValue("DisplayName", out displayName);
            this.DisplayName = displayName;

            string publisherDisplayName;

            packageProperties.GetStringValue("PublisherDisplayName", out publisherDisplayName);
            this.PublisherDisplayName = publisherDisplayName;

            // Get the min versions
            IAppxManifestReader3 appxManifestReader3 = (IAppxManifestReader3)appxManifestReader;
            IAppxManifestTargetDeviceFamiliesEnumerator targetDeviceFamiliesEnumerator = appxManifestReader3.GetTargetDeviceFamilies();

            while (targetDeviceFamiliesEnumerator.GetHasCurrent())
            {
                IAppxManifestTargetDeviceFamily targetDeviceFamily = targetDeviceFamiliesEnumerator.GetCurrent();
                this.TargetDeviceFamiliesMinVersions.Add(
                    targetDeviceFamily.GetName(),
                    new VersionInfo(targetDeviceFamily.GetMinVersion()));

                targetDeviceFamiliesEnumerator.MoveNext();
            }

            this.MinOSVersion = this.TargetDeviceFamiliesMinVersions.OrderBy(t => t.Value).FirstOrDefault().Value;

            this.PopulateCommonFields();
        }
Exemplo n.º 2
0
        public AppxMetadata(IRandomAccessStream randomAccessStream)
        {
            if (randomAccessStream == null)
            {
                throw new ArgumentNullException("file is null");
            }

            Guid    guid          = new Guid("0000000c-0000-0000-C000-000000000046");
            IStream packageStream = StreamUtils.CreateStreamOverRandomAccessStream(randomAccessStream, ref guid);

            IAppxFactory packageFactory = (IAppxFactory) new AppxFactory();

            packageFactory.CreatePackageReader(packageStream);

            this.AppxReader = packageFactory.CreatePackageReader(packageStream);

            this.BlockmapStream = this.AppxReader.GetBlockMap().GetStream();

            IAppxManifestReader appxManifestReader = this.AppxReader.GetManifest();

            this.ManifestStream = appxManifestReader.GetStream();
            IAppxManifestPackageId packageId = appxManifestReader.GetPackageId();

            this.PackageName       = packageId.GetName();
            this.PackageFamilyName = packageId.GetPackageFamilyName();
            this.PackageFullName   = packageId.GetPackageFullName();
            this.Publisher         = packageId.GetPublisher();
            this.Version           = new VersionInfo(packageId.GetVersion());

            IAppxManifestProperties packageProperties = appxManifestReader.GetProperties();
            string displayName;

            packageProperties.GetStringValue("DisplayName", out displayName);
            this.DisplayName = displayName;

            string publisherDisplayName;

            packageProperties.GetStringValue("PublisherDisplayName", out publisherDisplayName);
            this.PublisherDisplayName = publisherDisplayName;

            // Get the min versions
            IAppxManifestReader3 appxManifestReader3 = (IAppxManifestReader3)appxManifestReader;
            IAppxManifestTargetDeviceFamiliesEnumerator targetDeviceFamiliesEnumerator = appxManifestReader3.GetTargetDeviceFamilies();

            while (targetDeviceFamiliesEnumerator.GetHasCurrent())
            {
                IAppxManifestTargetDeviceFamily targetDeviceFamily = targetDeviceFamiliesEnumerator.GetCurrent();
                this.TargetDeviceFamiliesMinVersions.Add(
                    targetDeviceFamily.GetName(),
                    new VersionInfo(targetDeviceFamily.GetMinVersion()));

                targetDeviceFamiliesEnumerator.MoveNext();
            }

            this.MinOSVersion = this.TargetDeviceFamiliesMinVersions.OrderBy(t => t.Value).FirstOrDefault().Value;

            this.PopulateCommonFields();
        }
Exemplo n.º 3
0
        private IAppxFile GetManifestInfoFromPackage(IAppxFactory appxFactory, IAppxPackageReader packageReader)
        {
            var appxFile = packageReader.GetFootprintFile(APPX_FOOTPRINT_FILE_TYPE.MANIFEST);
            IAppxManifestReader manifestReader;

            appxFactory.CreateManifestReader(appxFile.GetStream(), out manifestReader);
            GetManifestInfo(manifestReader);
            return(appxFile);
        }
Exemplo n.º 4
0
 private void GetManifestInfoFromFile(IAppxFactory appxFactory, string manifestPath)
 {
     Console.WriteLine("Reading manifest info from: " + manifestPath);
     using (var stream = new StreamWrapper(File.OpenRead(manifestPath)))
     {
         IAppxManifestReader manifestReader;
         appxFactory.CreateManifestReader(stream, out manifestReader);
         GetManifestInfo(manifestReader);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes this instance of the AppxMetadata class from an msix stream.
        /// </summary>
        /// <param name="packageStream">the msix package stream.</param>
        private void Initialize(IStream packageStream)
        {
            IAppxFactory packageFactory = (IAppxFactory) new AppxFactory();

            this.AppxReader = packageFactory.CreatePackageReader(packageStream);

            this.BlockmapStream = this.AppxReader.GetBlockMap().GetStream();

            IAppxManifestReader appxManifestReader = this.AppxReader.GetManifest();

            this.ManifestStream = appxManifestReader.GetStream();
            IAppxManifestPackageId packageId = appxManifestReader.GetPackageId();

            this.PackageName       = packageId.GetName();
            this.PackageFamilyName = packageId.GetPackageFamilyName();
            this.PackageFullName   = packageId.GetPackageFullName();
            this.Publisher         = packageId.GetPublisher();
            this.Version           = new VersionInfo(packageId.GetVersion());

            IAppxManifestProperties packageProperties = appxManifestReader.GetProperties();
            string displayName;

            packageProperties.GetStringValue("DisplayName", out displayName);
            this.DisplayName = displayName;

            string publisherDisplayName;

            packageProperties.GetStringValue("PublisherDisplayName", out publisherDisplayName);
            this.PublisherDisplayName = publisherDisplayName;

            // Get the min versions
            IAppxManifestReader3 appxManifestReader3 = (IAppxManifestReader3)appxManifestReader;
            IAppxManifestTargetDeviceFamiliesEnumerator targetDeviceFamiliesEnumerator = appxManifestReader3.GetTargetDeviceFamilies();

            while (targetDeviceFamiliesEnumerator.GetHasCurrent())
            {
                IAppxManifestTargetDeviceFamily targetDeviceFamily = targetDeviceFamiliesEnumerator.GetCurrent();
                this.TargetDeviceFamiliesMinVersions.Add(
                    targetDeviceFamily.GetName(),
                    new VersionInfo(targetDeviceFamily.GetMinVersion()));

                targetDeviceFamiliesEnumerator.MoveNext();
            }

            this.MinOSVersion = this.TargetDeviceFamiliesMinVersions.OrderBy(t => t.Value).FirstOrDefault().Value;

            this.PopulateCommonFields();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets a package reader for the file, properly handling for encrypted packages.
        /// </summary>
        /// <param name="packagePath">The path to the package file.</param>
        /// <param name="isEncrypted">True if the package is encrypted; false if not.</param>
        /// <returns>A package reader for the file.</returns>
        public static IAppxPackageReader GetPackageReader(string packagePath, bool isEncrypted = false)
        {
            IAppxPackageReader result = null;

            if (isEncrypted)
            {
                IAppxEncryptionFactory factory = (IAppxEncryptionFactory) new AppxEncryptionFactory();
                result = factory.CreateEncryptedPackageReader(StreamUtils.CreateInputStreamOnFile(packagePath), System.IntPtr.Zero);
            }
            else
            {
                IAppxFactory factory = (IAppxFactory) new AppxFactory();
                result = factory.CreatePackageReader(StreamUtils.CreateInputStreamOnFile(packagePath));
            }

            return(result);
        }
Exemplo n.º 7
0
 private void GetManifestInfoFromFile(IAppxFactory appxFactory, string manifestPath)
 {
     Console.WriteLine("Reading manifest info from: " + manifestPath);
     using (var stream = new StreamWrapper(File.OpenRead(manifestPath)))
     {
         IAppxManifestReader manifestReader;
         appxFactory.CreateManifestReader(stream, out manifestReader);
         GetManifestInfo(manifestReader);
     }
 }
Exemplo n.º 8
0
 private IAppxFile GetManifestInfoFromPackage(IAppxFactory appxFactory, IAppxPackageReader packageReader)
 {
     var appxFile = packageReader.GetFootprintFile(APPX_FOOTPRINT_FILE_TYPE.MANIFEST);
     IAppxManifestReader manifestReader;
     appxFactory.CreateManifestReader(appxFile.GetStream(), out manifestReader);
     GetManifestInfo(manifestReader);
     return appxFile;
 }
        static public void GetPackagesForCurrentUser(ref List <PackageInfo> packageInfos)
        {
            string currentUserSID = WindowsIdentity.GetCurrent().User.ToString();

            //---------------------------------------------------------------------------------------
            // Get iterator over all packages installed for this user

            // First, we need the PackageManager

            IAppxFactory appxFactory = (IAppxFactory) new AppxFactory();

            PackageManager packageManager = new PackageManager();

            IEnumerable <Package> packages = packageManager.FindPackagesForUser(currentUserSID);
            int cPackages = packages.Count();

            if (cPackages == 0)
            {
                return;
            }

            packageInfos = new List <PackageInfo>(cPackages);
            foreach (Package package in packages)
            {
                try
                {
                    // Find and load manifest
                    string manifestPath = package.InstalledLocation.Path + "\\AppxManifest.xml";

                    AppxPackaging.IStream manifestStream;
                    if (SHCreateStreamOnFileEx(
                            manifestPath,
                            0x00000040, // STGM_READ | STGM_SHARE_DENY_NONE
                            0,          // file creation attributes
                            false,      // fCreate
                            null,       // reserved
                            out manifestStream) < 0)
                    {
                        // If we can't open the manifest for this package, skip it
                        continue;
                    }

                    IAppxManifestReader manifestReader;
                    manifestReader = appxFactory.CreateManifestReader(manifestStream);

                    IAppxManifestApplicationsEnumerator appsEnum = manifestReader.GetApplications();
                    if (appsEnum.GetHasCurrent() == 0)
                    {
                        // Packages with no apps exist, and are uninteresting.  Skip.
                        continue;
                    }

                    // Grab info from Package
                    PackageInfo packageInfo = new PackageInfo();
                    packageInfo.installedLocation = package.InstalledLocation.Path;

                    // Grab info from PackageID
                    PackageId packageId = package.Id;
                    packageInfo.architecture = packageId.Architecture.ToString();
                    packageInfo.fullName     = packageId.FullName;
                    packageInfo.name         = packageId.Name;
                    packageInfo.version      = String.Format(
                        "{0}.{1}.{2}.{3}",
                        packageId.Version.Major,
                        packageId.Version.Minor,
                        packageId.Version.Build,
                        packageId.Version.Revision);

                    IAppxManifestProperties props = manifestReader.GetProperties();
                    packageInfo.publisher = props.GetStringValue("PublisherDisplayName");

                    // Figure out temp folder path

                    if (!GetACInfo(packageId.FamilyName, out packageInfo.acSid, out packageInfo.tempDir))
                    {
                        continue;
                    }

                    // Check to see if CLRHost.dll is listed as an InProcessServer extension.  If so,
                    // it may be a JS/CLR hybrid package, in which case we'll include all the
                    // AppUserModelIds we find later on.
                    bool usesClrHostExtension = false;
                    try
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(manifestPath);
                        XPathNavigator      docNavigator = xmlDoc.CreateNavigator();
                        XmlNamespaceManager mgr          = new XmlNamespaceManager(docNavigator.NameTable);
                        mgr.AddNamespace("pm", "http://schemas.microsoft.com/appx/2010/manifest");
                        XPathNodeIterator iterator = docNavigator.Select("/pm:Package/pm:Extensions/pm:Extension/pm:InProcessServer/pm:Path", mgr);
                        foreach (XPathNavigator selectedNodeNavigator in iterator)
                        {
                            if (selectedNodeNavigator.Value.IndexOf("clrhost.dll", StringComparison.CurrentCultureIgnoreCase) != -1)
                            {
                                usesClrHostExtension = true;
                                break;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // Intentionally fall through. Any error encountered determining
                        // whether this is a hybrid package should just result in us
                        // assuming it isn't one.
                        Debug.Assert(!usesClrHostExtension);
                    }

                    // For each app in the package, get its App User Model ID
                    packageInfo.appInfoList = new List <AppInfo>(5);
                    while (appsEnum.GetHasCurrent() != 0)
                    {
                        IAppxManifestApplication app = appsEnum.GetCurrent();
                        AppInfo appInfo = new AppInfo();
                        appInfo.userModelId = app.GetAppUserModelId();
                        appInfo.exeName     = app.GetStringValue("Executable");
                        IAppxManifestResourcesEnumerator resourcesEnum = manifestReader.GetResources();
                        if ((appInfo.userModelId != null) &&
                            ((appInfo.exeName != null) || usesClrHostExtension))
                        {
                            // Only care about apps with an app user model ID
                            // (which we need for activation) and either an exe name (to display to user) or
                            // evidence that a CLR extension is used
                            if (appInfo.exeName == null)
                            {
                                appInfo.exeName = "(no executable)";
                            }
                            packageInfo.appInfoList.Add(appInfo);
                        }
                        appsEnum.MoveNext();
                    }

                    if (packageInfo.appInfoList.Count > 0)
                    {
                        // Only care about packages that contain apps we care about
                        packageInfos.Add(packageInfo);
                    }
                }
                catch (Exception)
                {
                    // If there are any problems with the package we're currently
                    // iterating over, just skip it and continue with the next package
                    // in the enumeration. For example, can't open the manifest
                    // or can't find installed location for the package (b/c the
                    // developer manually moved files around), skip it
                }
            }
        }
Exemplo n.º 10
0
        //Get uwp application details from package
        public static AppxDetails UwpGetAppxDetailsFromAppPackage(Package appPackage)
        {
            IStream      inputStream = null;
            IAppxFactory appxFactory = (IAppxFactory) new AppxFactory();
            AppxDetails  appxDetails = new AppxDetails();

            try
            {
                //Get detailed information from app package
                string appFamilyName = appPackage.Id.FamilyName;
                appxDetails.FullPackageName = appPackage.Id.FullName;
                appxDetails.InstallPath     = appPackage.InstalledLocation.Path;
                string manifestPath = appxDetails.InstallPath + "/AppXManifest.xml";
                //Debug.WriteLine("Reading uwp app manifest file: " + manifestPath);

                //Open the uwp application manifest file
                SHCreateStreamOnFileEx(manifestPath, STGM_MODES.STGM_SHARE_DENY_NONE, 0, false, IntPtr.Zero, out inputStream);
                if (inputStream != null)
                {
                    IAppxManifestReader      appxManifestReader      = appxFactory.CreateManifestReader(inputStream);
                    IAppxManifestApplication appxManifestApplication = appxManifestReader.GetApplications().GetCurrent();

                    //Get and set the application executable name
                    appxManifestApplication.GetStringValue("Executable", out string executableName);
                    appxDetails.ExecutableName = Path.GetFileName(executableName);

                    //Get and set the family name identifier
                    appxManifestApplication.GetStringValue("Id", out string appIdentifier);
                    appxDetails.FamilyNameId = appFamilyName + "!" + appIdentifier;

                    //Get and set the application display name
                    appxManifestApplication.GetStringValue("DisplayName", out string displayName);
                    appxDetails.DisplayName = UwpGetMsResourceString(appIdentifier, appxDetails.FullPackageName, displayName);

                    //Get all the available application logo images
                    appxManifestApplication.GetStringValue("Square30x30Logo", out appxDetails.Square30x30Logo);
                    appxManifestApplication.GetStringValue("Square70x70Logo", out appxDetails.Square70x70Logo);
                    appxManifestApplication.GetStringValue("Square150x150Logo", out appxDetails.Square150x150Logo);
                    appxManifestApplication.GetStringValue("Square310x310Logo", out appxDetails.Square310x310Logo);
                    appxManifestApplication.GetStringValue("Wide310x150Logo", out appxDetails.Wide310x150Logo);

                    //Check the largest available square logo
                    if (!string.IsNullOrWhiteSpace(appxDetails.Square310x310Logo))
                    {
                        appxDetails.SquareLargestLogoPath = Path.Combine(appxDetails.InstallPath, appxDetails.Square310x310Logo);
                    }
                    else if (!string.IsNullOrWhiteSpace(appxDetails.Square150x150Logo))
                    {
                        appxDetails.SquareLargestLogoPath = Path.Combine(appxDetails.InstallPath, appxDetails.Square150x150Logo);
                    }
                    else if (!string.IsNullOrWhiteSpace(appxDetails.Square70x70Logo))
                    {
                        appxDetails.SquareLargestLogoPath = Path.Combine(appxDetails.InstallPath, appxDetails.Square70x70Logo);
                    }
                    else if (!string.IsNullOrWhiteSpace(appxDetails.Square30x30Logo))
                    {
                        appxDetails.SquareLargestLogoPath = Path.Combine(appxDetails.InstallPath, appxDetails.Square30x30Logo);
                    }
                    string originalSquareLargestLogoPath = appxDetails.SquareLargestLogoPath;
                    appxDetails.SquareLargestLogoPath = UwpGetImageSizePath(appxDetails.SquareLargestLogoPath);

                    //Check if the file can be accessed
                    try
                    {
                        if (!string.IsNullOrWhiteSpace(appxDetails.SquareLargestLogoPath))
                        {
                            FileStream fileStream = File.OpenRead(appxDetails.SquareLargestLogoPath);
                            fileStream.Dispose();
                        }
                        else
                        {
                            appxDetails.SquareLargestLogoPath = originalSquareLargestLogoPath;
                        }
                    }
                    catch
                    {
                        //Debug.WriteLine("No permission to open: " + appxDetails.SquareLargestLogoPath);
                        appxDetails.SquareLargestLogoPath = originalSquareLargestLogoPath;
                    }

                    //Check the largest available wide logo
                    if (!string.IsNullOrWhiteSpace(appxDetails.Wide310x150Logo))
                    {
                        appxDetails.WideLargestLogoPath = Path.Combine(appxDetails.InstallPath, appxDetails.Wide310x150Logo);
                    }
                    string originalWideLargestLogoPath = appxDetails.WideLargestLogoPath;
                    appxDetails.WideLargestLogoPath = UwpGetImageSizePath(appxDetails.WideLargestLogoPath);

                    //Check if the file can be accessed
                    try
                    {
                        if (!string.IsNullOrWhiteSpace(appxDetails.WideLargestLogoPath))
                        {
                            FileStream fileStream = File.OpenRead(appxDetails.WideLargestLogoPath);
                            fileStream.Dispose();
                        }
                        else
                        {
                            appxDetails.WideLargestLogoPath = originalWideLargestLogoPath;
                        }
                    }
                    catch
                    {
                        //Debug.WriteLine("No permission to open: " + appxDetails.WideLargestLogoPath);
                        appxDetails.WideLargestLogoPath = originalWideLargestLogoPath;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed reading details from uwp manifest: " + appPackage.Id.FamilyName + "/" + ex.Message);
            }

            Marshal.ReleaseComObject(inputStream);
            Marshal.ReleaseComObject(appxFactory);
            return(appxDetails);
        }