/// <summary> /// Captures an image from a directory path and stores it in an image file. /// </summary> /// <param name="wimHandle">The handle to a .wim file returned by <see cref="CreateFile" />.</param> /// <param name="path">The root drive or directory path from where the image data is captured.</param> /// <param name="options">Specifies the features to use during the capture.</param> /// <returns>A <see cref="WimHandle"/> of the image if the method succeeded, otherwise false.</returns> /// <exception cref="ArgumentNullException">wimHandle is null. /// -or- /// path is null</exception> /// <exception cref="DirectoryNotFoundException"><paramref name="path"/> does not exist.</exception> /// <exception cref="Win32Exception">The Windows® Imaging API reported a failure.</exception> public static WimHandle CaptureImage(WimHandle wimHandle, string path, WimCaptureImageOptions options) { // See if the handle is null if (wimHandle == null) { throw new ArgumentNullException(nameof(wimHandle)); } // See if path is null if (path == null) { throw new ArgumentNullException(nameof(path)); } // See if path doesn't exist if (!Directory.Exists(path)) { throw new DirectoryNotFoundException($"Could not find part of the path '{path}'"); } // Call the native function WimHandle imageHandle = WimgApi.NativeMethods.WIMCaptureImage(wimHandle, path, (DWORD)options); // See if the handle returned is valid if (imageHandle == null || imageHandle.IsInvalid) { // Throw a Win32Exception which will call GetLastError throw new Win32Exception(); } // Return the handle to the image return(imageHandle); }
public bool CaptureImage( string wimFile, string imageName, string imageDescription, string imageFlag, string InputDirectory, string imageDisplayName = null, string imageDisplayDescription = null, WIMInformationXML.WINDOWS windows = null, WimCompressionType compressionType = WimCompressionType.Lzx, WimCaptureImageOptions addFlags = WimCaptureImageOptions.None, IImaging.ProgressCallback progressCallback = null) { string title = $"Creating {imageName} ({wimFile.Split('\\').Last()})"; try { int directoriesScanned = 0; int filesScanned = 0; WimMessageResult callback2(WimMessageType messageType, object message, object userData) { switch (messageType) { case WimMessageType.Process: { WimMessageProcess processMessage = (WimMessageProcess)message; if (processMessage.Path.StartsWith(Path.Combine(InputDirectory.EndsWith(":") ? InputDirectory + @"\" : InputDirectory, @"System Volume Information"), StringComparison.InvariantCultureIgnoreCase)) { processMessage.Process = false; } break; } case WimMessageType.Progress: { WimMessageProgress progressMessage = (WimMessageProgress)message; progressCallback?.Invoke($"{title} (Estimated time remaining: {progressMessage.EstimatedTimeRemaining})", progressMessage.PercentComplete, false); break; } case WimMessageType.Scanning: { WimMessageScanning scanningMessage = (WimMessageScanning)message; switch (scanningMessage.CountType) { case WimMessageScanningType.Directories: { directoriesScanned = scanningMessage.Count; break; } case WimMessageScanningType.Files: { filesScanned = scanningMessage.Count; break; } } progressCallback?.Invoke($"Scanning objects ({filesScanned} files, {directoriesScanned} directories scanned)", 0, true); break; } } return(WimMessageResult.Success); } using (var wimHandle = WimgApi.CreateFile( wimFile, WimFileAccess.Write, WimCreationDisposition.OpenAlways, compressionType == WimCompressionType.Lzms ? WimCreateFileOptions.Chunked : WimCreateFileOptions.None, compressionType)) { // Always set a temporary path // WimgApi.SetTemporaryPath(wimHandle, Environment.GetEnvironmentVariable("TEMP")); // Register a method to be called while actions are performed by WIMGAPi for this .wim file // WimgApi.RegisterMessageCallback(wimHandle, callback2); try { using (var imagehandle = WimgApi.CaptureImage(wimHandle, InputDirectory, addFlags)) { var wiminfo = WimgApi.GetImageInformationAsString(imagehandle); var wiminfoclass = WIMInformationXML.DeserializeIMAGE(wiminfo); if (!string.IsNullOrEmpty(imageFlag)) { wiminfoclass.FLAGS = imageFlag; } if (!string.IsNullOrEmpty(imageName)) { wiminfoclass.NAME = imageName; } if (!string.IsNullOrEmpty(imageDescription)) { wiminfoclass.DESCRIPTION = imageDescription; } if (!string.IsNullOrEmpty(imageDisplayName)) { wiminfoclass.DISPLAYNAME = imageDisplayName; } if (!string.IsNullOrEmpty(imageDisplayDescription)) { wiminfoclass.DISPLAYDESCRIPTION = imageDisplayDescription; } if (windows != null) { wiminfoclass.WINDOWS = windows; } wiminfo = WIMInformationXML.SerializeIMAGE(wiminfoclass); WimgApi.SetImageInformation(imagehandle, wiminfo); } } finally { // Be sure to unregister the callback method // WimgApi.UnregisterMessageCallback(wimHandle, callback2); } } } catch { return(false); } return(true); }