Exemple #1
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);
        }
Exemple #2
0
        public bool GetWIMImageInformation(
            string wimFile,
            int imageIndex,
            out WIMInformationXML.IMAGE image)
        {
            image = null;
            try
            {
                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))
                        {
                            var wiminfo = WimgApi.GetImageInformationAsString(imageHandle);
                            image = WIMInformationXML.DeserializeIMAGE(wiminfo);
                        }
                    }
                    finally
                    {
                    }
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Exemple #3
0
        public bool GetWIMInformation(
            string wimFile,
            out WIMInformationXML.WIM wim)
        {
            wim = null;
            try
            {
                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
                    {
                        var wiminfo = WimgApi.GetImageInformationAsString(wimHandle);
                        wim = WIMInformationXML.DeserializeWIM(wiminfo);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + " - " + ex.ToString());
                    }
                    finally
                    {
                    }
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Exemple #4
0
        public bool ExportImage(
            string wimFile,
            string destinationWimFile,
            int imageIndex,
            IEnumerable <string> referenceWIMs         = null,
            WimCompressionType compressionType         = WimCompressionType.Lzx,
            IImaging.ProgressCallback progressCallback = null)
        {
            return(WIMLibImaging.ExportImage(wimFile,
                                             destinationWimFile,
                                             imageIndex,
                                             referenceWIMs,
                                             compressionType,
                                             progressCallback));

            string title = $"Exporting {wimFile.Split('\\').Last()} - Index {imageIndex}";

            try
            {
                WimMessageResult callback2(WimMessageType messageType, object message, object userData)
                {
                    switch (messageType)
                    {
                    case WimMessageType.Progress:
                    {
                        WimMessageProgress progressMessage = (WimMessageProgress)message;
                        progressCallback?.Invoke($"{title} (Estimated time remaining: {progressMessage.EstimatedTimeRemaining})", progressMessage.PercentComplete, false);
                        break;
                    }
                    }

                    return(WimMessageResult.Success);
                }

                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"));

                    if (referenceWIMs != null)
                    {
                        foreach (var referenceFile in referenceWIMs)
                        {
                            WimgApi.SetReferenceFile(wimHandle, referenceFile, WimSetReferenceMode.Append, WimSetReferenceOptions.Chunked);
                        }
                    }

                    // Register a method to be called while actions are performed by WIMGAPi for this .wim file
                    //
                    WimgApi.RegisterMessageCallback(wimHandle, callback2);

                    using (var newWimHandle = WimgApi.CreateFile(
                               destinationWimFile,
                               WimFileAccess.Write,
                               WimCreationDisposition.OpenAlways,
                               compressionType == WimCompressionType.Lzms ? WimCreateFileOptions.Chunked : WimCreateFileOptions.None,
                               compressionType))
                    {
                        // Always set a temporary path
                        //
                        WimgApi.SetTemporaryPath(newWimHandle, Environment.GetEnvironmentVariable("TEMP"));

                        // Register a method to be called while actions are performed by WIMGAPi for this .wim file
                        //
                        WimgApi.RegisterMessageCallback(newWimHandle, callback2);

                        try
                        {
                            using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, imageIndex))
                            {
                                var    wiminfo      = WimgApi.GetImageInformationAsString(imageHandle);
                                var    wiminfoclass = WIMInformationXML.DeserializeIMAGE(wiminfo);
                                string imagename    = wiminfoclass.DISPLAYNAME == null ? wiminfoclass.NAME : wiminfoclass.DISPLAYNAME;

                                title = $"Exporting {imagename} ({wimFile.Split('\\').Last()} - Index {imageIndex})";

                                // Export the image contents
                                // This call is blocking but WIMGAPI will be calling MyCallbackMethod() during the process
                                //
                                WimgApi.ExportImage(imageHandle, newWimHandle, WimExportImageOptions.AllowDuplicates);
                            }
                        }
                        finally
                        {
                            // Be sure to unregister the callback method
                            //
                            WimgApi.UnregisterMessageCallback(wimHandle, callback2);

                            // Be sure to unregister the callback method
                            //
                            WimgApi.UnregisterMessageCallback(newWimHandle, callback2);
                        }
                    }
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Exemple #5
0
        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);
        }