The following example demonstrates using the AnswerFileComponentSetting class and some of its members. To run the example, you must have the install.wim file on your machine.

noteNote:
To run this example, you must change the value of the variable wimPath to match the path that contains the .wim file on your machine. If you are running the example at a command prompt, use the compilation switches provided on the second line. If you are running the example in the Microsoft Visual Studio Integrated Development Environment (IDE), add the references to those files shown on the second line.

Procedures

Declare your constants

  • Declare the path of the .wim file.

    private const string wimPath = 
    	@"C:\myWorkingFolder\install.wim";
    

Create an answer file associated with an image

  • Get a WimInfo instance for a .wim file.

    WimInfo imageInfo = Cpi.Instance.OpenWim(wimPath);
    
  • Get the corresponding OfflineImage instance.

    OfflineImage image =
    Cpi.Instance.OpenOfflineImage(imageInfo.Images[0]);
    
  • Create an answer file associated with the OfflineImage.

    AnswerFile answerFile = Cpi.Instance.CreateAnswerFile(image);
    

Create an event handler

  • Create an event handler for the answer file.

    answerFile.ConfirmOverwriteXml += new
    	EventHandler<MergeXmlEventArgs>(
    	answerFile_ConfirmOverwriteXml);
    

Create asynchronous command overrides

  • Find the identity of Deployment from the image.

    Identity identity = image.FindComponentIdentity(
    	"Microsoft-Windows-Deployment");
    
  • Add the Deployment component csPass1 to the answer file in the specialize pass.

    AnswerFileComponentSetting csPass1 =
    	answerFile.ComponentSettings.Create(identity,
    	ConfigurationPass.specialize, 
    	CreateOptions.ReturnExisting);
    
  • Add three asynchronous command overrides to the component in the specialize pass.

    AnswerFileSettingOverride soCommand1 =
    	csPass1.SettingOverrides.Create(
    	"RunAsynchronous/RunAsynchronousCommand",
    	CreateOptions.FailIfExists);
    soCommand1.SettingOverrides.Create("Order", 
    	CreateOptions.ReturnExisting).Value = "1";
    soCommand1.SettingOverrides.Create("Path", 
    	CreateOptions.ReturnExisting).Value =
    	@"C:\Utils\util1.exe";
    
  • Confirm the three asynchronous command overrides in the specialize pass.

    AnswerFileSettingOverride soCommandList1 =
    	csPass1.SettingOverrides.FindAny("RunAsynchronous");
    
  • Get the XML data for the added overrides.

    string xmlData = soCommandList1.GetXml();
    
  • Add another Deployment component (cPass2) to the answer file in the auditUser pass.

  • Merge the XML data for the overrides from the specialize pass into the auditUser pass.

    csPass2.MergeXml(xmlData, true);
    
  • Confirm that there are now three asynchronous command overrides in the auditUser pass.

    AnswerFileSettingOverride soCommandList2 =
    	csPass2.SettingOverrides.FindAny("RunAsynchronous");
    
  • Confirm that the first asynchronous command in the auditUser pass is C:\Utils\util1.exe.

    AnswerFileSettingOverride soCommand =
    	csPass2.SettingOverrides.FindAny(
    	"RunAsynchronous/RunAsynchronousCommand[Order=\"1\"]");
    AnswerFileSettingOverride soCommandPath =
    	soCommand.SettingOverrides.FindAny("Path");
    

Example

// AnswerFileComponentSetting.cs

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

class Program
{
	public static void Main()
	{
		Program prog = new Program();
		prog.GetXmlMergeXmlSample();
}

	public void GetXmlMergeXmlSample()
	{
		// Get a WimInfo instance for a .wim file:
		string wimPath = @"C:\myWorkingFolder\install.wim";
		WimInfo imageInfo = Cpi.Instance.OpenWim(wimPath);

		// Ensure the catalog is up to date:
		if (!imageInfo.Images[0].ValidateCatalog().Succeeded)
			imageInfo.Images[0].CreateCatalog();

		// Get the corresponding OfflineImage instance 
		// for the .wim file:
		OfflineImage image =
			Cpi.Instance.OpenOfflineImage(
			imageInfo.Images[0]);

		// Create an answer file associated with the OfflineImage:
		AnswerFile answerFile =
			Cpi.Instance.CreateAnswerFile(image);
		answerFile.ConfirmOverwriteXml += new
		 EventHandler<MergeXmlEventArgs>(
		 answerFile_ConfirmOverwriteXml);

		// Find the identity of the Deployment component
		// from the image:
		Identity identity = image.FindComponentIdentity(
			"Microsoft-Windows-Deployment");

		// Add the Deployment component to the answer file 
		// in the specialize pass:
		AnswerFileComponentSetting csPass1 =
			answerFile.ComponentSettings.Create(identity,
			ConfigurationPass.specialize,
			CreateOptions.ReturnExisting);

		// Create three asynchronous command overrides to
		// the component in the specialize pass:
		AnswerFileSettingOverride soCommand1 =
		 csPass1.SettingOverrides.Create(
		 "RunAsynchronous/RunAsynchronousCommand",
		 CreateOptions.FailIfExists);
		soCommand1.SettingOverrides.Create("Order",
			CreateOptions.ReturnExisting).Value = "1";
		soCommand1.SettingOverrides.Create("Path",
			CreateOptions.ReturnExisting).Value =
			@"C:\Utils\util1.exe";
		AnswerFileSettingOverride soCommand2 =
			csPass1.SettingOverrides.Create(
			"RunAsynchronous/RunAsynchronousCommand",
			CreateOptions.FailIfExists);
		soCommand2.SettingOverrides.Create("Order",
			CreateOptions.ReturnExisting).Value = "2";
		soCommand2.SettingOverrides.Create("Path",
			CreateOptions.ReturnExisting).Value =
			@"C:\Utils\util2.exe";
		AnswerFileSettingOverride soCommand3 =
			csPass1.SettingOverrides.Create(
			"RunAsynchronous/RunAsynchronousCommand",
			CreateOptions.FailIfExists);
		soCommand3.SettingOverrides.Create("Order",
			CreateOptions.ReturnExisting).Value = "3";
		soCommand3.SettingOverrides.Create("Path",
			CreateOptions.ReturnExisting).Value =
			@"C:\Utils\util2.exe";

		// This will confirm there are now three asynchronous
		// command overrides in the specialize pass:
		AnswerFileSettingOverride soCommandList1 =
			csPass1.SettingOverrides.FindAny("RunAsynchronous");
		Console.WriteLine(
			"The number of asynchronous commands in " +
			"the specialize pass is: {0}",
			soCommandList1.ChildSettingOverrides.Count.ToString());

		// Get the XML data for the added overrides:
		string xmlData = soCommandList1.GetXml();

		// Add the Deployment component to the answer file in 
		// the auditUser pass:
		AnswerFileComponentSetting csPass2 =
			answerFile.ComponentSettings.Create(identity,
			ConfigurationPass.auditUser,
			CreateOptions.ReturnExisting);

		// Merge the XML data for the overrides from the specialize
		// pass into the auditUser pass:
		csPass2.MergeXml(xmlData, true);

		// This will confirm there are now three asynchronous 
		// command overrides in the auditUser pass:
		AnswerFileSettingOverride soCommandList2 =
			csPass2.SettingOverrides.FindAny("RunAsynchronous");
		Console.WriteLine(
			"The number of asynchronous commands in " +
			"the auditUser pass is: {0}",
			soCommandList2.ChildSettingOverrides.Count.ToString());

		// This will confirm that the first asynchronous command in 
		// the auditUser pass is "C:\Utils\util1.exe":
		AnswerFileSettingOverride soCommand =
			csPass2.SettingOverrides.FindAny(
			"RunAsynchronous/RunAsynchronousCommand[Order=\"1\"]");
		AnswerFileSettingOverride soCommandPath =
		soCommand.SettingOverrides.FindAny("Path");
		Console.WriteLine(
			"The RunAsynchronous command that runs first in " +
			"the auditUser pass is: {0}",
			soCommandPath.Value);
}

	void answerFile_ConfirmOverwriteXml(
		object sender, MergeXmlEventArgs e)
	{
		// Answering YesToAll causes event firing to stop for
		// a particular merge operation:
		e.ConfirmationResult = ConfirmOverwriteResult.YesToAll;
}
}

Output

The number of asynchronous commands in the specialize pass is: 3
The number of asynchronous commands in the auditUser pass is: 3
The RunAsynchronous command that runs first in the auditUser pass is: C:\Utils\util1.exe

See Also