///<summary> ///Public constructor to create a WIM object ///</summary> ///<param name="imageFilePath">Path of .wim file to create or to open.</param> ///<param name="mode">Specifies Open, Create, Create/Open disposition of the .wim file.</param> ///<param name="access">Specifies access level of Read Only or Write.</param> //[CLSCompliant(false)] public WindowsImageContainer(string imageFilePath, CreateFileMode mode, CreateFileAccess access) { CreateFileAccessPrivate fileAccess = GetMappedFileAccess(access); if (fileAccess == CreateFileAccessPrivate.Read && (!File.Exists(imageFilePath) || (CreateFileMode.OpenExisting != mode))) { throw new System.UnauthorizedAccessException(string.Format(CultureInfo.CurrentCulture, "Read access can be specified only with OpenExisting mode or OpenAlways mode when the .wim file does not exist.")); } // //Imaging DLLs must be in the same directory. // try { m_ImageContainerHandle = NativeMethods.CreateFile(imageFilePath, (uint)fileAccess, (uint)mode); m_WindowsImageFilePath = imageFilePath; } catch (System.DllNotFoundException ex) { throw new System.DllNotFoundException(string.Format(CultureInfo.CurrentCulture, "Unable to load WIM libraries. Make sure the correct DLLs are present (Wimgapi.dll and Xmlrw.dll)."), ex.InnerException); } if (!m_ImageContainerHandle.Equals(IntPtr.Zero)) { // //Set the temporary path so that we can write to an image. This //cannot be %TEMP% as it does not exist on Windows PE // string tempDirectory = System.Environment.GetEnvironmentVariable("systemdrive"); NativeMethods.SetTemporaryPath(m_ImageContainerHandle, tempDirectory); } else { // //Throw an exception // throw new System.InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Unable to open the .wim file {0}.", imageFilePath)); } // //Finally, we must hook into the events. // m_MessageCallback = new NativeMethods.MessageCallback(ImageEventMessagePump); NativeMethods.RegisterCallback(m_MessageCallback); }
GetMappedFileAccess(CreateFileAccess access) { // //Map the file access specified from an int to a uint. // CreateFileAccessPrivate fileAccess; switch (access) { case CreateFileAccess.Read: fileAccess = CreateFileAccessPrivate.Read; break; case CreateFileAccess.Write: fileAccess = CreateFileAccessPrivate.Write; break; default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "No file access level specified.")); } return fileAccess; }