コード例 #1
0
ファイル: AppxMetadata.cs プロジェクト: vors/MSIX-Toolkit
        /// <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();
        }
コード例 #2
0
    private static bool GetBoolValue(IAppxManifestProperties props, string name)
    {
        bool value;

        props.GetBoolValue(name, out value);
        return(value);
    }
コード例 #3
0
ファイル: AppxMetadata.cs プロジェクト: vors/MSIX-Toolkit
        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();
        }
コード例 #4
0
            public static string GetStringValue(IAppxManifestProperties props, string name)
            {
                if (props == null)
                {
                    return(null);
                }

                props.GetStringValue(name, out string value);
                return(value);
            }
コード例 #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();
        }
コード例 #6
0
        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
                }
            }
        }
コード例 #7
0
 public static bool GetBoolValue(IAppxManifestProperties props, string name)
 {
     props.GetBoolValue(name, out bool value);
     return(value);
 }