In this walkthrough, you start by checking whether an old distribution share folder exists. You then change the attributes of the files in that folder to Normal and delete the folder. Finally, you import a package from a source folder that contains the .cab files.

Procedures

Declare your constants

  • Declare the following two constants as fields in your class. The first contains the path of the distribution share folder, and the second is the source folder that contains the .cab file.

    private const string distributionSharePath =
    @"C:\DistributionShare";
    private const string cabFilesPath =
    @"C:\Packages";
    

Delete the old distribution share

  • Check whether there is an old distribution share folder by using the following condition.

    if (Directory.Exists(distributionSharePath))
    
  • If it exists, delete the directory after setting the attributes of files to Normal.

    Directory.Delete(distributionSharePath, true);
    

Create new folders in the distribution share

  • Set the path for the packages folder.

    string packagesFolder = Path.Combine(distributionSharePath,
    DistributionShareInfo.PackagesFolder);
    
  • Set the paths for the subfolders, such as out-of-box drivers and $OEM$.

    string oemFolder = Path.Combine(distributionSharePath,
    DistributionShareInfo.OemFolder);
    
  • Create the packages folder and its subfolders.

    Directory.CreateDirectory(oemFolder);
    

Import the packages

  • Set the packages folder string:

    string packagesFolder = Path.Combine(distributionSharePath,
    DistributionShareInfo.PackagesFolder);
    
  • Get the source path for Calc.

    string calc = Path.Combine(cabFilesPath, "Calc.cab");
    
  • Import Calc.

    DistributionShareInfo.ImportPackage(calc, distributionSharePath);
    
  • Get the source files and check to make sure they were imported to the correct location.

    string calcResult = Path.Combine(Path.Combine(
    Path.Combine(packagesFolder, "HotFix"),
    "x86_Microsoft.Windows.Calc.Demo_1.0.0.0_en_6595b64144ccf1df_"),
    "calc.cab");
    

Example

The following example shows how to import packages from a source folder to a specific distribution share folder.

noteNote:
To run this example, you must change the value of distributionSharePath to match the path of the existing distribution share folder on your machine, if any. You must also change the value of cabFilesPath to match the path of the folder that contains the .cab files on your machine.
// ImportPackages.cs

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

namespace ImportPackages
{
	public class ImportPackage
	{
		private const string distributionSharePath = 
			@"C:\DistributionShare";
		private const string cabFilesPath =
			@"C:\Packages";

		static void Main()
		{
			CreateDistributionShare();
			// Deletion complete:
			Console.WriteLine("Old folders have been deleted.");

			// Success message:
			Console.WriteLine("The disribution share is created at {0}",
				distributionSharePath);

			ImportAPackage();
			Console.WriteLine("The package is imported from: {0}",
				cabFilesPath);
	}

		private static void CreateDistributionShare()
		{
			// If exists, delete the old one.
			if (Directory.Exists(distributionSharePath))
			{
				string[] filenames =
				 Directory.GetFiles(distributionSharePath, "*",
				 SearchOption.AllDirectories);
				foreach (string filename in filenames)
					File.SetAttributes(filename,
					FileAttributes.Normal);
				Directory.Delete(distributionSharePath, true);
		}

			// Creating new folders:
			// Define path of a new folder for packages:
			string packagesFolder = Path.Combine(distributionSharePath,
				DistributionShareInfo.PackagesFolder);

			// Define path of out-of-box drivers:
			string oobDriver = Path.Combine(distributionSharePath, 
				DistributionShareInfo.OutOfBoxDriversFolder);

			// Define path of $OEM$:
			string oemFolder = Path.Combine(distributionSharePath,
				DistributionShareInfo.OemFolder);

			// Create the three defining directories of
			// a distribution share:
			Directory.CreateDirectory(packagesFolder);
			Directory.CreateDirectory(oobDriver);
			Directory.CreateDirectory(oemFolder);
	}

		public static void ImportAPackage()
		{
			// Set the packages folder string:
			string packagesFolder = Path.Combine(distributionSharePath,
				DistributionShareInfo.PackagesFolder);

			// Get the source path for "Calc":
			string calc = Path.Combine(cabFilesPath, "Calc.cab");

			// Import the "Calc" package:
			DistributionShareInfo.ImportPackage(calc, 
				distributionSharePath);

			// Get the source files and check to make sure 
			// they were imported to the correct location:
			string calcResult = Path.Combine(Path.Combine(
				Path.Combine(packagesFolder, "HotFix"),
				"x86_Microsoft.Windows.Calc.Demo_1.0.0.0_en_6595b64144ccf1df_"),
				"calc.cab");

			// Get the source path for "Clock":
			string clock = Path.Combine(cabFilesPath, 
				"ClockQfe.cab");

			// Import the "Clock" package:
			DistributionShareInfo.ImportPackage(clock, 
				distributionSharePath);

			// Get the source files and check to make sure 
			// they were imported to the correct location:
			string clockResultFolder = Path.Combine(Path.Combine(
				packagesFolder, "HotFix"), 
				"x86_Clock.Demo.qfe_1.0.0.0_en_6595b64144ccf1df_");

			// Get the source path for "LangPack":
			string langpack = Path.Combine(cabFilesPath,
				"LangPack.cab");

			// Import the "Clock" package:
			DistributionShareInfo.ImportPackage(langpack,
				distributionSharePath);

			// Get the source files and make sure 
			// they were imported to the correct location:
			string langpackResultFolder = Path.Combine(
				Path.Combine(packagesFolder, "LanguagePack"),
				"x86_Microsoft-Windows-WindowsFoundation-LanguagePack-Package" + 
				"_6.0.5210.0_en-US_31bf3856ad364e35_nonSxS");

			// Get the source path for "OOB":
			string oob = Path.Combine(cabFilesPath, "OOB.cab");

			// Import the "oob" package:
			DistributionShareInfo.ImportPackage(oob, 
				distributionSharePath);

			// Get the source files and make sure 
			// they were imported to the correct location:
			string oobResultFolder = Path.Combine(Path.Combine(
				packagesFolder, "HotFix"), 
				"x86_Clock.Demo_1.0.0.0_en_6595b64144ccf1df_");

			// Get the source path for "SP":
			string sp = Path.Combine(cabFilesPath, "SP.cab");

			// Import the "SP" package:
			DistributionShareInfo.ImportPackage(sp, 
				distributionSharePath);

			// Get the source files and make sure 
			// they were imported to the correct location:
			string spResultFolder = Path.Combine(
				Path.Combine(packagesFolder, "HotFix"),
				"x86_Microsoft-Windows-Kernel32-QFE_6.0.5096.1" + 
				"_neutral_31bf3856ad364e35_");
	}
}
}

Output

Old folders have been deleted.
The disribution share is created at C:\DistributionShare
The package is imported from: C:\Packages

See Also