This walkthrough contains several methods to perform various actions with packages, such as testing package properties, enumerating a package, or determining whether a package file or an ID is installed. The following procedures describe each of these methods.

Procedures

Declare your constants

  • Define the folders that contain the .wim file and the packages by declaring them as constant fields in the class.

    private const string wimPath =
    	@"C:\myWorkingFolder\install.wim";
    private const string packagesPath =
    	@"C:\DistributionShare\Packages\HotFix\calc.package";
    
    These constants will be used by each method in the class.

Create a CPI instance and an offline image

  • Create a CPI instance.

    Cpi n = Cpi.Instance;
    
  • Get the Windows image information for the image in the .wim file.

    WimInfo wimInfo = n.OpenWim(wimfile);
    
  • Create an OfflineImage object.

    OfflineImage image = n.OpenOfflineImage(wInfo.Images[0]);
    

Check if a package is installed

  • Open a package from packagesPath.

    Package pkg = n.OpenPackage(packagesPath);
    
  • Check if a package file is installed.

    if (image.Packages.Contains(pkg.Id))
    	Console.WriteLine(string.Format(
    	"{0} is in the image.", pkg.Path));
    else
    	Console.WriteLine(string.Format(
    	"{0} is not in the image.", pkg.Path));
    
  • Check for a package with a specified identity, and then create the identity.

    Identity idCalc = new Identity("Microsoft.Windows.Calc.Demo",
    	"31bf3856ad364e35", new Version("1.0.0.0"), "x86", "en", null);
    
  • Search for the package.

    if (image.Packages.Contains(idCalc))
    	Console.WriteLine(
    	"The Microsoft.Windows.Calc.Demo package type is: "
    	+ image.Packages[idCalc].ReleaseType);
    else
    	Console.WriteLine(
    	"Microsoft.Windows.Calc.Demo is not in the image.");
    

Enumerate the package and list its items

  • Enumerate and display the package items:

    foreach (Package pkg in image.Packages)
    {
    	Console.WriteLine("Package name: " + pkg.ProductName);
    	Console.WriteLine("Package name: " + pkg.ReleaseType);
    }
    

Get a Windows foundation package

  • Get a Windows Foundation package.

    Identity foundationId = image.WindowsFoundationPackageId;
    Package foundationPackage = image.Packages[foundationId];
    

Find a package by name

  • Search for the package by its name:

    Identity packageId = image.FindPackageIdentity(
    	"Microsoft.Windows.Calc.Demo", "en");
    

Example

The following example demonstrates several methods that are used to manage packages.

noteNote:
To run this example, you must change the value of the constant field myWorkingFolder to match the path that contains the .wim files and change packagesPath to match the path that contains the Calc package on your machine.
// ManagingPackages.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ComponentStudio.ComponentPlatformInterface;

namespace ManagingPackages
{
	public class ManagePackage
	{
		private const string wimPath =
			@"C:\myWorkingFolder\install.wim";

		private const string packagesPath =
			@"C:\DistributionShare\Packages\HotFix\calc.package";

		public void HowToCheckIfAPackageFileIsInstalled()
		{

			// Create a Cpi instance:
			Cpi n = Cpi.Instance;
		
			// Open the package:
			Package pkg = n.OpenPackage(packagesPath);

			// Get wim info and check if the catalog file is created,
			// if not create one:
			WimInfo wInfo = n.OpenWim(wimPath);
			if (!wInfo.Images[0].ValidateCatalog().Succeeded)
				wInfo.Images[0].CreateCatalog();

			// Open the offline image:
			OfflineImage image = n.OpenOfflineImage(wInfo.Images[0]);

			// Search for the package:
			if (image.Packages.Contains(pkg.Id))
				Console.WriteLine(string.Format(
					"{0} is in the image.", pkg.Path));
			else
			{
				Console.WriteLine(string.Format(
					"{0} is not in the image.", pkg.Path));
		}
	}

		public void HowToDetermineTypeOfAPackage()
		{
			// Create identity of the package:
			Identity idCalc = new Identity(
				"Microsoft.Windows.Calc.Demo",
				"31bf3856ad364e35", new Version("1.0.0.0"),
				"x86", "en", null);
			Identity idClock = new Identity(
				"Clock.Demo.qfe",
				"6595b64144ccf1df", new Version("1.0.0.0"),
				"x86", "en", null);

			// Create a Cpi instance:
			Cpi n = Cpi.Instance;

			// Get .wim info and check if the catalog file is created,
			// if not create one:
			WimInfo wInfo = n.OpenWim(wimPath);
			if (!wInfo.Images[0].ValidateCatalog().Succeeded)
				wInfo.Images[0].CreateCatalog();

			// Open the offline image:
			OfflineImage image = n.OpenOfflineImage(wInfo.Images[0]);   

			// Search for the packages:
			if (image.Packages.Contains(idCalc))
				Console.WriteLine(
				"The Microsoft.Windows.Calc.Demo package type is: "
				+ image.Packages[idCalc].ReleaseType);
			else
				Console.WriteLine(
				"Microsoft.Windows.Calc.Demo is not in the image.");

			// Search for the packages
			if (image.Packages.Contains(idClock))
				Console.WriteLine(
				"The Clock.Demo.qfe package type is: " +
					image.Packages[idClock].ReleaseType);
			else
				Console.WriteLine(
				"Microsoft.Windows.Clock.Demo is not in the image.");
	}

		public void HowToEnumeratePackages()
		{
			// Create a Cpi instance:
			Cpi n = Cpi.Instance;

			// Get .wim info and check if the catalog file is created,
			// if not create one:
			WimInfo wInfo = n.OpenWim(wimPath);
			if (!wInfo.Images[0].ValidateCatalog().Succeeded)
				wInfo.Images[0].CreateCatalog();

			// Open the offline image:
			OfflineImage image = n.OpenOfflineImage(wInfo.Images[0]);   

			foreach (Package pkg in image.Packages)
			{
				Console.WriteLine("Package name: "+ pkg.ProductName);
				Console.WriteLine("Package name: "+ pkg.ReleaseType);
		}
	}

		public void HowToCheckIfAPackageIdIsInstalled()
		{
			// Create identity of the package:
			Identity idCalc = new Identity(
				"Microsoft.Windows.Calc.Demo",
				"31bf3856ad364e35", new Version("1.0.0.0"),
				"x86", "en", null);
			Identity idClock = new Identity("Clock.Demo.qfe", 
				"6595b64144ccf1df", new Version("1.0.0.0"),
				"x86", "en", null);

			// Create a Cpi instance:
			Cpi n = Cpi.Instance;

			// Get .wim information and check if the catalog
			// file is created, if not create one:
			WimInfo wInfo = n.OpenWim(wimPath);
			if (!wInfo.Images[0].ValidateCatalog().Succeeded)
				wInfo.Images[0].CreateCatalog();

			// Open the offline image:
			OfflineImage image = n.OpenOfflineImage(wInfo.Images[0]);   

			// Search for the package:
			if (image.Packages.Contains (idCalc))
				Console.WriteLine(
				"Microsoft.Windows.Calc.Demo is in the image.");
			else
				Console.WriteLine(
				"Microsoft.Windows.Calc.Demo is not in the image.");
	} 

		public void HowToGetWindowsFoundationPackage()
		{
			// Create a Cpi instance:
			Cpi n = Cpi.Instance;

			// Get .wim info and check if the catalog file is created,
			// if not create one:
			WimInfo wInfo = n.OpenWim(wimPath);
			if (!wInfo.Images[0].ValidateCatalog().Succeeded)
				wInfo.Images[0].CreateCatalog();

		 // Open the offline image:
		 OfflineImage image = n.OpenOfflineImage(wInfo.Images[0]);   
		 // Get the Windows Foundation ID:
		 Identity foundationId = image.WindowsFoundationPackageId;
		 Package foundationPackage = image.Packages[foundationId];
		 Console.WriteLine("Foundation Id: {0}", foundationId);
	}

		public void HowToFindAPackageByName()
		{
			// Create a Cpi instance:
			Cpi n = Cpi.Instance;

			// Get .wim info and check if the catalog file is created,
			// if not create one:
			WimInfo wInfo = n.OpenWim(wimPath);
			if (!wInfo.Images[0].ValidateCatalog().Succeeded)
				wInfo.Images[0].CreateCatalog();

		 // Open the offline image:
		 OfflineImage image = n.OpenOfflineImage(wInfo.Images[0]);   

			// Define package ID:
			Identity packageId = image.FindPackageIdentity(
			 "Microsoft.Windows.Calc.Demo", "en");
	}
					 
		public static void Main()
		{
			ManagePackage myObj = new ManagePackage(); 	
			myObj.HowToEnumeratePackages(); 
			myObj.HowToGetWindowsFoundationPackage();
			myObj.HowToFindAPackageByName();
			myObj.HowToDetermineTypeOfAPackage();
			myObj.HowToCheckIfAPackageIdIsInstalled();
			myObj.HowToCheckIfAPackageFileIsInstalled();
	}
	
}
}

Output

Package name: Client-Help-Package
Package name: LanguagePack
...
...
Package name: Server-Help-Package.clientcontentrole
Package name: FeaturePack
Foundation Id: x86_Microsoft-Windows-Foundation-Package_6.0.5322.0__31bf3856ad36
4e35_
Microsoft.Windows.Calc.Demo is not in the image
Microsoft.Windows.Clock.Demo is not in the image
Microsoft.Windows.Calc.Demo is not in the image
C:\DistributionShare\Packages\HotFix\calc.package is not in the image

See Also