示例#1
0
        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);
        }
示例#2
0
        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);
                }
            }
        }
示例#3
0
        public void LoadImageTest_ThrowsIndexOutOfRangeException_indexZero()
        {
            var indexOutOfRangeException = Should.Throw<IndexOutOfRangeException>(() =>
                WimgApi.LoadImage(TestWimHandle, 0));

            indexOutOfRangeException.Message.ShouldBe("There is no image at index 0.");
        }
示例#4
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);
        }
示例#5
0
        public void MountImageTest_ThrowsWin32Exception_imageIndexOutOfRange()
        {
            Win32Exception invalidParameterException = Should.Throw <Win32Exception>(() =>
                                                                                     WimgApi.MountImage(MountPath, TestWimPath, 10));

            invalidParameterException.Message.ShouldBe("The parameter is incorrect");
        }
示例#6
0
        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);
                    }
                }
            }
        }
示例#7
0
        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();
                    }
                }
            });
        }
示例#8
0
        public void GetMountedImageHandleTest_ThrowsArgumentNullException_mountPath()
        {
            WimHandle wimHandle;

            ShouldThrow <ArgumentNullException>("mountPath", () =>
                                                WimgApi.GetMountedImageHandle(null, false, out wimHandle));
        }
示例#9
0
        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();
                    }
                }
            });
        }
示例#10
0
        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);
        }
示例#11
0
        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);
        }
示例#12
0
        public void DeleteImageTest_ThrowsIndexOutOfRangeException_indexOutOfRange()
        {
            var indexOutOfRangeException = Should.Throw<IndexOutOfRangeException>(() =>
                WimgApi.DeleteImage(TestWimHandle, 10));

            indexOutOfRangeException.Message.ShouldBe("There is no image at index 10.");
        }
示例#13
0
        public void SplitFileMinimumSizeTest()
        {
            string partPath = Path.Combine(TestDirectory, "out.wim");

            long partSize = WimgApi.SplitFile(TestWimHandle, partPath);

            partSize.ShouldNotBe(0);
        }
示例#14
0
        public void SplitFileMinimumSizeTest()
        {
            var partPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, "out.wim");

            var partSize = WimgApi.SplitFile(TestWimHandle, partPath);

            partSize.ShouldNotBe(0);
        }
示例#15
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));
            }
        }
示例#17
0
        public void ExportImageTest_ThrowsArgumentNullException_wimHandle()
        {
            using (var imageHandle = WimgApi.LoadImage(TestWimHandle, 1))
            {
                var imageHandleCopy = imageHandle;

                ShouldThrow <ArgumentNullException>("wimHandle", () =>
                                                    WimgApi.ExportImage(imageHandleCopy, null, WimExportImageOptions.None));
            }
        }
示例#18
0
        public void ExtractImagePathTest_ThrowsArgumentNullException_destinationFile()
        {
            using (var imageHandle = WimgApi.LoadImage(TestWimHandle, 1))
            {
                var imageHandleCopy = imageHandle;

                ShouldThrow <ArgumentNullException>("destinationFile", () =>
                                                    WimgApi.ExtractImagePath(imageHandleCopy, "", null));
            }
        }
示例#19
0
        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));
            }
        }
示例#20
0
        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");
        }
示例#22
0
        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;
         }
     }
 }
示例#24
0
        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);
        }
示例#25
0
 public void RegisterLogFileTest()
 {
     WimgApi.RegisterLogFile(_logFilePath);
     try
     {
         File.Exists(_logFilePath).ShouldBeTrue();
     }
     finally
     {
         WimgApi.UnregisterLogFile(_logFilePath);
     }
 }
示例#26
0
        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);
                }
            }
        }
示例#27
0
        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);
        }
示例#28
0
        public void MountImageTest()
        {
            WimgApi.MountImage(MountPath, TestWimPath, 1);
            try
            {
                WimMountInfoCollection mountedImages = WimgApi.GetMountedImageInfo();

                mountedImages.Count.ShouldBe(1);
            }
            finally
            {
                WimgApi.UnmountImage(MountPath, TestWimPath, 1, false);
            }
        }
示例#29
0
        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);
                }
            }
        }
示例#30
0
        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);
        }