public bool SetWIMImageInformation( string wimFile, int imageIndex, WIMInformationXML.IMAGE image) { try { using (var wimHandle = WimgApi.CreateFile( wimFile, WimFileAccess.Write, WimCreationDisposition.OpenExisting, WimCreateFileOptions.Chunked, WimCompressionType.None)) { // Always set a temporary path // WimgApi.SetTemporaryPath(wimHandle, Environment.GetEnvironmentVariable("TEMP")); try { using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, imageIndex)) { string img = WIMInformationXML.SerializeIMAGE(image); WimgApi.SetImageInformation(imageHandle, img); } } finally { } } } catch { return(false); } return(true); }
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); }