This walkthrough contains two methods: the first adds actions and credentials to access the drivers, and the second removes the driver through the answer file instance and verifies that driver path is updated accordingly.

Procedures

Declare your constants

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

    private const string ansFile = 
       @"C:\myWorkingFolder\My Unattend.xml";
    private const string wimPath = @"C:\myWorkingFolder\install.wim";
    

Create the offline image and the answer file

  • 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]);
    
  • Create the answer file.

    unattend = n.CreateAnswerFile(image);
    

Add the driver action

  • Add a driver action to the answer file.

    DriverPathCollection driverCollection =
       unattend.GetDriverPaths(ConfigurationPass.auditSystem);
    nvidiaPath = driverCollection.Create(
       @"\\server1\share1\drivers\nvidia\GForce4",
       CreateOptions.ReturnExisting);
    

Save the answer file

  • Save the answer file.

    unattend.SaveAs(AddDriverUnattend);
    

Remove the driver

  • Repeat the steps for adding a driver, then use the following code to remove the driver.

    Identity dmiId = image.FindComponentIdentity(DmiName);
    AnswerFileSettingOverride so =
    unattend.SettingOverrides.FindAny(dmiId,
    nvidiaPath.Path, ConfigurationPass.auditSystem);
    so.Remove();
    

Example

The following example demonstrates how to add and remove drivers.

noteNote:
To run this example, you must change the value of the variable wimPath to match the path that contains the .wim files on your machine. You must also change the variable ansFile as appropriate.
// ManageDrivers.cs

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

namespace MyDriverNameSpace
{
	public class MyDriverClass
	{
		static void Main()
		{
			MyDriverClass mDC = new MyDriverClass();
			mDC.AddDriverPath();
			Console.WriteLine(
				"The driver is added to the answer file.");
			mDC.RemoveDriverPath();
			Console.WriteLine(
				"The driver has been removed from the answer file.");
	}

		private const string DmiName =
		"Microsoft-Windows-PnpCustomizationsNonWinPE";

		private const string ansFile =
		@"C:\myWorkingFolder\My Unattend.xml";

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

		public void AddDriverPath()
		{
			DriverPath nvidiaPath;
			AnswerFile unattend;

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

			// 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]);

			// Create the answer file:
			unattend = n.CreateAnswerFile(image);
			// Add a driver action to add nvidia driver:
			DriverPathCollection driverCollection = 
			unattend.GetDriverPaths(ConfigurationPass.auditSystem);
			nvidiaPath = driverCollection.Create(
			@"\\server1\share1\drivers\nvidia\GForce4",
			CreateOptions.ReturnExisting);

			// Optional: add credential to access the driver:
			nvidiaPath.DynamicKey = "nvidia1";
			nvidiaPath.Domain = "domain1";
			nvidiaPath.UserName = "user1";
			nvidiaPath.Password = "password1";

			// Save the file:
			unattend.SaveAs(ansFile);
	}

		public void RemoveDriverPath()
		{
			DriverPath nvidiaPath;
			DriverPath ati;
			AnswerFile unattend;
			string wimPath = @"C:\myWorkingFolder\install.wim";

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

			// 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]);

			// Create the answer file:
			unattend = n.CreateAnswerFile(image);

			// Add a driver action to add nvidia driver.
			DriverPathCollection driverPaths =
		 unattend.GetDriverPaths(ConfigurationPass.auditSystem);
			nvidiaPath = driverPaths.Create(
			@"\\server1\share1\drivers\nvidia\GForce4",
			CreateOptions.ReturnExisting);

			// Optional: add credential to access the driver:
			nvidiaPath.DynamicKey = "nvidia1";
			nvidiaPath.Domain = "domain1";
			nvidiaPath.UserName = "user1";
			nvidiaPath.Password = "password1";
			ati = driverPaths.Create(
			@"\\server1\share1\drivers\ati\radeon",
			CreateOptions.ReturnExisting);
			ati.DynamicKey = "ati1";

			// Try to remove the nvidia driver via answer file instance 
			// and verify that the driver path is updated accordingly:
			Identity dmiId = image.FindComponentIdentity(DmiName);
			AnswerFileSettingOverride so =
			unattend.SettingOverrides.FindAny(dmiId,
			nvidiaPath.Path, ConfigurationPass.auditSystem);
			so.Remove();
	}
}
}

Output

The driver is added to the answer file.
The driver is removed from the answer file.

See Also