/// <summary> /// Loads a volume image from a Windows® image (.wim) file. /// </summary> /// <param name="wimHandle">A <see cref="WimHandle"/> of a .wim file returned by the <see cref="CreateFile"/> method.</param> /// <param name="index">The one-based index of the image to load. An image file may store multiple images.</param> /// <returns>A <see cref="WimHandle"/> representing the volume image.</returns> /// <exception cref="ArgumentNullException">wimHandle is null.</exception> /// <exception cref="IndexOutOfRangeException">index is less than 1 /// -or- /// index is greater than the number of images in the Windows® imaging file.</exception> /// <exception cref="Win32Exception">The Windows® Imaging API reported a failure.</exception> /// <remarks>You must call the <see cref="SetTemporaryPath"/> method before calling the <see cref="LoadImage"/> method so the image metadata can be extracted and processed from the temporary location.</remarks> public static WimHandle LoadImage(WimHandle wimHandle, int index) { // See if wimHandle is null if (wimHandle == null) { throw new ArgumentNullException(nameof(wimHandle)); } // See if the specified index is valid if (index < 1 || index > WimgApi.GetImageCount(wimHandle)) { throw new IndexOutOfRangeException($"There is no image at index {index}."); } // Call the native function WimHandle imageHandle = WimgApi.NativeMethods.WIMLoadImage(wimHandle, (DWORD)index); if (imageHandle == null || imageHandle.IsInvalid) { // Throw a Win32Exception based on the last error code throw new Win32Exception(); } // Return the image handle return(imageHandle); }
/// <summary> /// Marks the image with the given image index as bootable. /// </summary> /// <param name="wimHandle">A <see cref="WimHandle"/> of a Windows® image (.wim) file returned by the <see cref="CreateFile"/> method.</param> /// <param name="imageIndex">The one-based index of the image to load. An image file can store multiple images.</param> /// <exception cref="ArgumentNullException">wimHandle is null.</exception> /// <exception cref="IndexOutOfRangeException">index is less than 1 or greater than the number of images in the Windows® image file.</exception> /// <exception cref="Win32Exception">The Windows® Imaging API reported a failure.</exception> /// <remarks>If imageIndex is zero, then none of the images in the .wim file are marked for boot. At any time, only one image in a .wim file can be set to be bootable.</remarks> public static void SetBootImage(WimHandle wimHandle, int imageIndex) { // See if wimHandle is null if (wimHandle == null) { throw new ArgumentNullException(nameof(wimHandle)); } // See if the specified index is valid if (imageIndex < 1 || imageIndex > WimgApi.GetImageCount(wimHandle)) { throw new IndexOutOfRangeException($"There is no image at index {imageIndex}."); } // Call the native function if (!WimgApi.NativeMethods.WIMSetBootImage(wimHandle, (DWORD)imageIndex)) { // Throw a Win32Exception based on the last error code throw new Win32Exception(); } }