public static ObservableCollection <_WimImage> GetWIM(string filePath) { //Check registry first then return; var images = new ObservableCollection <_WimImage>(); using ( var handle = WimgApi.CreateFile(filePath, WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None)) { WimgApi.SetTemporaryPath(handle, Environment.GetEnvironmentVariable("TEMP")); var result = WimgApi.GetImageInformation(handle); var xParent = (XElement)result.FirstNode; var xElement = xParent.Element("TOTALBYTES"); if (xElement == null) { throw new Exception("Invalid WIM File."); } foreach (var xImage in xParent.Elements().Skip(1)) { var WI = new WimImage(xImage, filePath); images.Add(WI); } } return(images); }
public void ApplyImageTest_Abort() { using (WimHandle wimHandle = WimgApi.CreateFile(TestWimPath, WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None)) { WimgApi.SetTemporaryPath(wimHandle, TempPath); WimMessageResult MessageCallback(WimMessageType messageType, object message, object userData) => messageType == WimMessageType.Process ? WimMessageResult.Abort : WimMessageResult.Done; WimgApi.RegisterMessageCallback(wimHandle, MessageCallback); try { using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1)) { WimHandle imageHandleCopy = imageHandle; Should.Throw <OperationCanceledException>(() => WimgApi.ApplyImage(imageHandleCopy, ApplyPath, WimApplyImageOptions.NoApply)); } } finally { WimgApi.UnregisterMessageCallback(wimHandle, MessageCallback); } } }
public void LoadImageTest_ThrowsIndexOutOfRangeException_indexZero() { var indexOutOfRangeException = Should.Throw<IndexOutOfRangeException>(() => WimgApi.LoadImage(TestWimHandle, 0)); indexOutOfRangeException.Message.ShouldBe("There is no image at index 0."); }
private bool ReseatWIMXml(string wimFile) { 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")); string xmldata = WimgApi.GetImageInformationAsString(wimHandle); var xml = WIMInformationXML.DeserializeWIM(xmldata); xmldata = WIMInformationXML.SerializeWIM(xml); WimgApi.SetImageInformation(wimHandle, xmldata); } } catch { return(false); } return(true); }
public void MountImageTest_ThrowsWin32Exception_imageIndexOutOfRange() { Win32Exception invalidParameterException = Should.Throw <Win32Exception>(() => WimgApi.MountImage(MountPath, TestWimPath, 10)); invalidParameterException.Message.ShouldBe("The parameter is incorrect"); }
internal static void ExecuteAgainstMountedImage(string imagePath, string mountPath, string tempPath, bool readOnly, Action <WimHandle, WimHandle> action) { using (WimHandle wimHandle = WimgApi.CreateFile(imagePath, WimFileAccess.Read | WimFileAccess.Write | WimFileAccess.Mount, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None)) { WimgApi.SetTemporaryPath(wimHandle, tempPath); using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1)) { WimMountImageOptions flags = WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix; if (readOnly) { flags |= WimMountImageOptions.ReadOnly; } WimgApi.MountImage(imageHandle, mountPath, flags); try { action?.Invoke(wimHandle, imageHandle); } finally { WimgApi.UnmountImage(imageHandle); } } } }
public void GetMountedImageHandleTest_ReadOnly() { ExecuteAgainstMountedImage((wimHandle, imageHandle) => { WimHandle actualImageHandle; using (var actualWimHandle = WimgApi.GetMountedImageHandle(MountPath, true, out actualImageHandle)) { try { actualWimHandle.ShouldNotBeNull(); actualImageHandle.ShouldNotBeNull(); var wimMountInfo = WimgApi.GetMountedImageInfoFromHandle(actualImageHandle); wimMountInfo.ShouldNotBeNull(); wimMountInfo.ImageIndex.ShouldBe(1); wimMountInfo.MountPath.ShouldBe(MountPath); wimMountInfo.Path.ShouldBe(TestWimPath); wimMountInfo.ReadOnly.ShouldBeTrue(); wimMountInfo.State.ShouldBe(WimMountPointState.Mounted); } finally { actualImageHandle.Dispose(); } } }); }
public void GetMountedImageHandleTest_ThrowsArgumentNullException_mountPath() { WimHandle wimHandle; ShouldThrow <ArgumentNullException>("mountPath", () => WimgApi.GetMountedImageHandle(null, false, out wimHandle)); }
public void GetMountedImageHandleTest_ReadWrite() { ExecuteAgainstMountedImage((wimHandle, imageHandle) => { using (WimHandle actualWimHandle = WimgApi.GetMountedImageHandle(MountPath, false, out WimHandle actualImageHandle)) { try { actualWimHandle.ShouldNotBeNull(); actualImageHandle.ShouldNotBeNull(); WimMountInfo wimMountInfo = WimgApi.GetMountedImageInfoFromHandle(actualImageHandle); wimMountInfo.ShouldNotBeNull(); wimMountInfo.ImageIndex.ShouldBe(1); wimMountInfo.MountPath.ShouldBe(MountPath, StringCompareShould.IgnoreCase); wimMountInfo.Path.ShouldBe(TestWimPath); wimMountInfo.ReadOnly.ShouldBeTrue(); wimMountInfo.State.ShouldBe(WimMountPointState.Mounted); } finally { actualImageHandle.Dispose(); } } }); }
public bool ExtractFileFromImage(string wimFile, int imageIndex, string fileToExtract, string destination) { return(WIMLibImaging.ExtractFileFromImage(wimFile, imageIndex, fileToExtract, destination)); using (var wimHandle = WimgApi.CreateFile( wimFile, WimFileAccess.Read, 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)) { WimgApi.ExtractImagePath(imageHandle, fileToExtract, destination); } } catch (Exception ex) { // A priv is not held by the client if (ex.HResult == -2147467259) { return(WIMLibImaging.ExtractFileFromImage(wimFile, imageIndex, fileToExtract, destination)); } return(false); } } return(true); }
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 DeleteImageTest_ThrowsIndexOutOfRangeException_indexOutOfRange() { var indexOutOfRangeException = Should.Throw<IndexOutOfRangeException>(() => WimgApi.DeleteImage(TestWimHandle, 10)); indexOutOfRangeException.Message.ShouldBe("There is no image at index 10."); }
public void SplitFileMinimumSizeTest() { string partPath = Path.Combine(TestDirectory, "out.wim"); long partSize = WimgApi.SplitFile(TestWimHandle, partPath); partSize.ShouldNotBe(0); }
public void SplitFileMinimumSizeTest() { var partPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, "out.wim"); var partSize = WimgApi.SplitFile(TestWimHandle, partPath); partSize.ShouldNotBe(0); }
public void SetBootImageTest() { const int bootImageIndex = TestWimTemplate.ImageCount; WimgApi.SetBootImage(TestWimHandle, bootImageIndex); WimgApi.GetAttributes(TestWimHandle).BootIndex.ShouldBe(bootImageIndex); }
public void ExtractImagePathTest_ThrowsArgumentNullException_sourceFile() { using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) { WimHandle imageHandleCopy = imageHandle; ShouldThrow <ArgumentNullException>("sourceFile", () => WimgApi.ExtractImagePath(imageHandleCopy, null, string.Empty)); } }
public void ExportImageTest_ThrowsArgumentNullException_wimHandle() { using (var imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) { var imageHandleCopy = imageHandle; ShouldThrow <ArgumentNullException>("wimHandle", () => WimgApi.ExportImage(imageHandleCopy, null, WimExportImageOptions.None)); } }
public void ExtractImagePathTest_ThrowsArgumentNullException_destinationFile() { using (var imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) { var imageHandleCopy = imageHandle; ShouldThrow <ArgumentNullException>("destinationFile", () => WimgApi.ExtractImagePath(imageHandleCopy, "", null)); } }
public void MountImageHandleTest_ThrowsDirectoryNotFoundException_mountPath() { using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) { WimHandle imageHandleCopy = imageHandle; Should.Throw <DirectoryNotFoundException>(() => WimgApi.MountImage(imageHandleCopy, Path.Combine(TestDirectory, Guid.NewGuid().ToString()), WimMountImageOptions.None)); } }
public void MountImageHandleTest_ThrowsArgumentNullException_mountPath() { using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) { WimHandle imageHandleCopy = imageHandle; ShouldThrow <ArgumentNullException>("mountPath", () => WimgApi.MountImage(imageHandleCopy, null, WimMountImageOptions.None)); } }
public void CopyFileTest_ThrowsWin32Exception_FailIfExists() { WimgApi.CopyFile(TestWimPath, _destinationPath, WimCopyFileOptions.None); File.Exists(_destinationPath).ShouldBeTrue(); Win32Exception win32Exception = Should.Throw <Win32Exception>(() => WimgApi.CopyFile(TestWimPath, _destinationPath, WimCopyFileOptions.FailIfExists)); win32Exception.Message.ShouldBe("The file exists"); }
public void LoadImageTest() { using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) { imageHandle.ShouldNotBeNull(); imageHandle.IsInvalid.ShouldBeFalse(); imageHandle.IsClosed.ShouldBeFalse(); } }
private void InitializeWIM() { using (WimHandle fullWIM = WimgApi.CreateFile(SourceWIM, WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.Chunked, WimCompressionType.None)) { XPathNodeIterator iterator = WimgApi.GetImageInformation(fullWIM).CreateNavigator().Select("/WIM/IMAGE/NAME"); AvailableEditions = new string[iterator.Count]; while (iterator.MoveNext()) { AvailableEditions[iterator.CurrentPosition - 1] = "[" + iterator.Current.SelectSingleNode("../@INDEX").Value + "] " + iterator.Current.Value; } } }
public void SetImageInformationTest() { var xmlDocument = new XmlDocument(); var fragment = xmlDocument.CreateDocumentFragment(); fragment.InnerXml = @"<WIM><TEST>This is a test</TEST></WIM>"; xmlDocument.AppendChild(fragment); WimgApi.SetImageInformation(TestWimHandle, xmlDocument); }
public void RegisterLogFileTest() { WimgApi.RegisterLogFile(_logFilePath); try { File.Exists(_logFilePath).ShouldBeTrue(); } finally { WimgApi.UnregisterLogFile(_logFilePath); } }
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); } } }
public void ApplyImageTest() { using (WimHandle wimHandle = WimgApi.CreateFile(TestWimPath, WimFileAccess.Read | WimFileAccess.Write | WimFileAccess.Mount, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None)) { WimgApi.SetTemporaryPath(wimHandle, TempPath); using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1)) { WimgApi.ApplyImage(imageHandle, ApplyPath, WimApplyImageOptions.Index | WimApplyImageOptions.DisableDirectoryAcl | WimApplyImageOptions.DisableFileAcl | WimApplyImageOptions.DisableRPFix); } } Directory.EnumerateFiles(ApplyPath).Count().ShouldBe(TestWimTemplate.FileCount); }
public void MountImageTest() { WimgApi.MountImage(MountPath, TestWimPath, 1); try { WimMountInfoCollection mountedImages = WimgApi.GetMountedImageInfo(); mountedImages.Count.ShouldBe(1); } finally { WimgApi.UnmountImage(MountPath, TestWimPath, 1, false); } }
public void SetReferenceFileTest() { var splitWims = CreateSplitWim().ToArray(); using (var wimHandle = WimgApi.CreateFile(splitWims[0], WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None)) { WimgApi.SetTemporaryPath(wimHandle, TempPath); foreach (var referenceFile in splitWims.Skip(1)) { WimgApi.SetReferenceFile(wimHandle, referenceFile, WimSetReferenceMode.Append, WimSetReferenceOptions.None); } } }
public void GetAttributesTest() { WimInfo wimInfo = WimgApi.GetAttributes(TestWimHandle); wimInfo.ShouldNotBeNull(); wimInfo.Attributes.ShouldBe(WimInfoAttributes.Normal); wimInfo.BootIndex.ShouldBe(0); wimInfo.CompressionType.ShouldBe(WimCompressionType.Lzx); wimInfo.Guid.ShouldNotBe(Guid.Empty); wimInfo.ImageCount.ShouldBe(TestWimTemplate.ImageCount); wimInfo.PartNumber.ShouldBe(1); wimInfo.TotalParts.ShouldBe(1); }