// 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();
}
}
}
|