public void CaptureImageWithCallbackTest() { CallbackObject userData = new CallbackObject(); using (WimHandle wimHandle = WimgApi.CreateFile(CaptureWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, WimCompressionType.Xpress)) { WimgApi.SetTemporaryPath(wimHandle, TempPath); WimgApi.RegisterMessageCallback(wimHandle, CaptureImageWithCallbackTestCallback, userData); try { using (WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, CapturePath, WimCaptureImageOptions.None)) { } } finally { WimgApi.UnregisterMessageCallback(wimHandle, CaptureImageWithCallbackTestCallback); } } _captureWithCallbackCalled.ShouldBe(true, "The callback should have been called"); userData.WasCalled.ShouldBe(true, "The callback should have set user data"); _captureWithCallbackFileCount.ShouldBe(TestWimTemplate.FileCount); }
public void CaptureImageTest() { using (WimHandle wimHandle = WimgApi.CreateFile(CaptureWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, WimCompressionType.Xpress)) { WimgApi.SetTemporaryPath(wimHandle, TempPath); using (WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, CapturePath, WimCaptureImageOptions.None)) { int imageCount = WimgApi.GetImageCount(wimHandle); imageCount.ShouldBe(1); } } }
private string CaptureTemplateImage(string capturePath) { string imagePath = Path.Combine(_testWimTemplateDirectory, TestWimTemplateFilename); if (!Directory.Exists(capturePath)) { throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, "Could not find part of the path '{0}'", capturePath)); } XmlDocument xmlDocument = new XmlDocument(); using (WimHandle wimHandle = WimgApi.CreateFile(imagePath, WimFileAccess.Write, WimCreationDisposition.CreateNew, WimCreateFileOptions.None, WimCompressionType.Lzx)) { WimgApi.SetTemporaryPath(wimHandle, _testWimTempPath); for (int i = 0; i < ImageCount; i++) { // ReSharper disable once UnusedVariable using (WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, capturePath, WimCaptureImageOptions.DisableDirectoryAcl | WimCaptureImageOptions.DisableFileAcl | WimCaptureImageOptions.DisableRPFix)) { } } XPathNavigator xml = WimgApi.GetImageInformation(wimHandle).CreateNavigator(); xml.ShouldNotBeNull(); // ReSharper disable once PossibleNullReferenceException xmlDocument.LoadXml(xml.OuterXml); XmlNodeList imageNodes = xmlDocument.SelectNodes("//WIM/IMAGE"); imageNodes.ShouldNotBeNull(); // ReSharper disable once PossibleNullReferenceException foreach (XmlElement imageNode in imageNodes) { XmlDocumentFragment fragment = xmlDocument.CreateDocumentFragment(); fragment.InnerXml = @"<WINDOWS> <ARCH>0</ARCH> <PRODUCTNAME>Microsoft® Windows® Operating System</PRODUCTNAME> <EDITIONID>WindowsPE</EDITIONID> <INSTALLATIONTYPE>WindowsPE</INSTALLATIONTYPE> <PRODUCTTYPE>WinNT</PRODUCTTYPE> <PRODUCTSUITE></PRODUCTSUITE> <LANGUAGES> <LANGUAGE>en-US</LANGUAGE> <DEFAULT>en-US</DEFAULT> </LANGUAGES> <VERSION> <MAJOR>6</MAJOR> <MINOR>3</MINOR> <BUILD>9600</BUILD> <SPBUILD>16384</SPBUILD> <SPLEVEL>0</SPLEVEL> </VERSION> <SYSTEMROOT>WINDOWS</SYSTEMROOT> </WINDOWS>"; imageNode.AppendChild(fragment); fragment.InnerXml = $@"<NAME>Test Image {imageNode.Attributes["INDEX"].Value}</NAME>"; imageNode.AppendChild(fragment); fragment.InnerXml = $@"<DESCRIPTION>Test Image {imageNode.Attributes["INDEX"].Value}</DESCRIPTION>"; imageNode.AppendChild(fragment); WimgApi.SetImageInformation(wimHandle, xmlDocument); } } return imagePath; }
public void CaptureImageTest_ThrowsDirectoryNotFoundException_path() { Should.Throw <DirectoryNotFoundException>(() => WimgApi.CaptureImage(TestWimHandle, Path.Combine(TestDirectory, Guid.NewGuid().ToString()), WimCaptureImageOptions.None)); }
public void CaptureImageTest_ThrowsArgumentNullException_wimHandle() { ShouldThrow <ArgumentNullException>("wimHandle", () => WimgApi.CaptureImage(null, TempPath, WimCaptureImageOptions.None)); }
public void CaptureImageTest_ThrowsArgumentNullException_path() { ShouldThrow <ArgumentNullException>("path", () => WimgApi.CaptureImage(TestWimHandle, null, WimCaptureImageOptions.None)); }
private string CaptureTemplateImage(string capturePath) { string imagePath = Path.Combine(_testWimTemplateDirectory, TestWimTemplateFilename); if (!Directory.Exists(capturePath)) { throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, "Could not find part of the path '{0}'", capturePath)); } XmlDocument xmlDocument = new XmlDocument { XmlResolver = null, }; using (WimHandle wimHandle = WimgApi.CreateFile(imagePath, WimFileAccess.Write, WimCreationDisposition.CreateNew, WimCreateFileOptions.None, WimCompressionType.Lzx)) { WimgApi.SetTemporaryPath(wimHandle, _testWimTempPath); for (int i = 0; i < ImageCount; i++) { // ReSharper disable once UnusedVariable using (WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, capturePath, WimCaptureImageOptions.DisableDirectoryAcl | WimCaptureImageOptions.DisableFileAcl | WimCaptureImageOptions.DisableRPFix)) { } } XPathNavigator xml = WimgApi.GetImageInformation(wimHandle).CreateNavigator(); xml.ShouldNotBeNull(); using (StringReader stringReader = new StringReader(xml.OuterXml)) using (XmlTextReader reader = new XmlTextReader(stringReader) { DtdProcessing = DtdProcessing.Prohibit, }) { xmlDocument.Load(reader); } XmlNodeList imageNodes = xmlDocument.SelectNodes("//WIM/IMAGE"); imageNodes.ShouldNotBeNull(); // ReSharper disable once PossibleNullReferenceException foreach (XmlElement imageNode in imageNodes) { XmlDocumentFragment fragment = xmlDocument.CreateDocumentFragment(); fragment.InnerXml = $@"<WINDOWS> <ARCH>{Architecture:D}</ARCH> <PRODUCTNAME>{ProductName}</PRODUCTNAME> <EDITIONID>{EditionId}</EDITIONID> <INSTALLATIONTYPE>{InstallationType}</INSTALLATIONTYPE> <PRODUCTTYPE>{ProductType}</PRODUCTTYPE> <PRODUCTSUITE></PRODUCTSUITE> <LANGUAGES> <LANGUAGE>{Language}</LANGUAGE> <DEFAULT>{DefaultLangauge}</DEFAULT> </LANGUAGES> <VERSION> <MAJOR>{ProductVersion.Major}</MAJOR> <MINOR>{ProductVersion.Minor}</MINOR> <BUILD>{ProductVersion.Build}</BUILD> <SPBUILD>{ProductVersion.Revision}</SPBUILD> <SPLEVEL>{SpLevel}</SPLEVEL> </VERSION> <SYSTEMROOT>{SystemRoot}</SYSTEMROOT> </WINDOWS>"; imageNode.AppendChild(fragment); fragment.InnerXml = $@"<NAME>{ImageNamePrefix}{imageNode.Attributes["INDEX"].Value}</NAME>"; imageNode.AppendChild(fragment); fragment.InnerXml = $@"<DESCRIPTION>{ImageNamePrefix}{imageNode.Attributes["INDEX"].Value}</DESCRIPTION>"; imageNode.AppendChild(fragment); WimgApi.SetImageInformation(wimHandle, xmlDocument); } } return(imagePath); }
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); }