public void WriteToFile(string FileName, bool Overwrite = true) { IFileSystemImageResult Res = ISO.CreateResultImage(); IStream ImgStream = (IStream)Res.ImageStream; if (ImgStream != null) { STATSTG Stat; ImgStream.Stat(out Stat, 0x1); if (File.Exists(FileName)) { if (Overwrite) { File.Delete(FileName); } else { throw new Exception("File already exists: " + FileName); } } IStream OutStream; SHCreateStreamOnFile(FileName, 0x1001, out OutStream); ImgStream.CopyTo(OutStream, Stat.cbSize, IntPtr.Zero, IntPtr.Zero); OutStream.Commit(0); } }
/// <summary> /// Burns data files to disc in a single session using files from a /// single directory tree. /// </summary> /// <param name="recorder">Burning device.</param> /// <param name="path">Directory of files to burn.</param> /// <param name="clientName">The friendly name of the client (used to determine recorder reservation conflicts).</param> public void BurnDirectory(DiscRecorder recorder, string path, string clientName = "IMAPI Client Record Name") { if (!recorder.MediaImage.IsRecorderSupported(recorder.Recorder)) { throw new Exception("The recorder is not supported"); } if (!recorder.MediaImage.IsCurrentMediaSupported(recorder.Recorder)) { throw new Exception("The current media is not supported"); } // Set the client name. recorder.MediaImage.ClientName = clientName; // Create an image stream for a specified directory. // Create a new file system image and retrieve the root directory IFileSystemImage fsi = new MsftFileSystemImage(); // Set the media size fsi.FreeMediaBlocks = recorder.MediaImage.FreeSectorsOnMedia; // Use legacy ISO 9660 Format fsi.FileSystemsToCreate = FsiFileSystems.FsiFileSystemISO9660; // Add the directory to the disc file system IFsiDirectoryItem dir = fsi.Root; dir.AddTree(path, false); // Write the progress. if (recorder.ProgressAction != null) { recorder.ProgressAction("Writing content to disc."); } // Create an image from the file system. // Data stream sent to the burning device IFileSystemImageResult result = fsi.CreateResultImage(); IStream stream = result.ImageStream; DiscFormat2Data_Events progress = recorder.MediaImage as DiscFormat2Data_Events; progress.Update += new DiscFormat2Data_EventsHandler(recorder.DiscFormat2Data_ProgressUpdate); // Write the image stream to disc using the specified recorder. recorder.MediaImage.Write(stream); // Burn the stream to disc progress.Update -= new DiscFormat2Data_EventsHandler(recorder.DiscFormat2Data_ProgressUpdate); // Write the progress. if (recorder.ProgressAction != null) { recorder.ProgressAction("Finished writing content."); } }
/// <summary> /// Burns data files to disc in a single session using files from a /// single directory tree. /// </summary> /// <param name="recorder">Burning Device. Must be initialized.</param> /// <param name="path">Directory of files to burn.</param> public void BurnDirectory(IDiscRecorder2 recorder, String path) { // Define the new disc format and set the recorder IDiscFormat2Data dataWriterImage = new MsftDiscFormat2Data(); dataWriterImage.Recorder = recorder; if (!dataWriterImage.IsRecorderSupported(recorder)) { Console.WriteLine("The recorder is not supported"); return; } if (!dataWriterImage.IsCurrentMediaSupported(recorder)) { Console.WriteLine("The current media is not supported"); return; } dataWriterImage.ClientName = "IMAPI Sample"; // Create an image stream for a specified directory. // Create a new file system image and retrieve the root directory IFileSystemImage fsi = new MsftFileSystemImage(); // Set the media size fsi.FreeMediaBlocks = dataWriterImage.FreeSectorsOnMedia; // Use legacy ISO 9660 Format fsi.FileSystemsToCreate = FsiFileSystems.FsiFileSystemISO9660; // Add the directory to the disc file system IFsiDirectoryItem dir = fsi.Root; dir.AddTree(path, false); // Create an image from the file system Console.WriteLine("Writing content to disc..."); IFileSystemImageResult result = fsi.CreateResultImage(); // Data stream sent to the burning device IStream stream = result.ImageStream; DiscFormat2Data_Events progress = dataWriterImage as DiscFormat2Data_Events; progress.Update += new DiscFormat2Data_EventsHandler(DiscFormat2Data_ProgressUpdate); // Write the image stream to disc using the specified recorder. dataWriterImage.Write(stream); // Burn the stream to disc progress.Update -= new DiscFormat2Data_EventsHandler(DiscFormat2Data_ProgressUpdate); Console.WriteLine("----- Finished writing content -----"); }
/// <summary>Burns data files to disc in a single session using files from a single directory tree.</summary> /// <param name="recorder">Burning Device. Must be initialized.</param> /// <param name="path">Directory of files to burn.</param> public static void BurnDirectory(this IDiscRecorder2 recorder, string path, string imageName = "IMAPI Sample") { // Define the new disc format and set the recorder var dataWriterImage = new IDiscFormat2Data { Recorder = recorder }; if (!dataWriterImage.IsRecorderSupported(recorder)) { Console.WriteLine("The recorder is not supported"); return; } if (!dataWriterImage.IsCurrentMediaSupported(recorder)) { Console.WriteLine("The current media is not supported"); return; } dataWriterImage.ClientName = imageName; // Create an image stream for a specified directory. // Create a new file system image and retrieve the root directory var fsi = new IFileSystemImage { // Set the media size FreeMediaBlocks = dataWriterImage.FreeSectorsOnMedia, // Use legacy ISO 9660 Format FileSystemsToCreate = FsiFileSystems.FsiFileSystemUDF }; // Add the directory to the disc file system IFsiDirectoryItem dir = fsi.Root; Console.WriteLine(); Console.Write("Adding files to image:".PadRight(80)); using (var eventDisp = new ComConnectionPoint(fsi, new DFileSystemImageEventsSink(AddTreeUpdate))) dir.AddTree(path, false); Console.WriteLine(); // Create an image from the file system Console.WriteLine("\nWriting content to disc..."); IFileSystemImageResult result = fsi.CreateResultImage(); // Data stream sent to the burning device IStream stream = result.ImageStream; // Write the image stream to disc using the specified recorder. using (var eventDisp = new ComConnectionPoint(dataWriterImage, new DDiscFormat2DataEventsSink(WriteUpdate))) dataWriterImage.Write(stream); // Burn the stream to disc Console.WriteLine("----- Finished writing content -----");
public void BurnCD() { #region CD WRITING MsftDiscMaster2 discMaster = null; discMaster = new MsftDiscMaster2(); String path = "D://hello2.txt"; Console.WriteLine("Writing to the disc"); if (!discMaster.IsSupportedEnvironment) { return; } foreach (string uniqueRecorderId in discMaster) { var discRecorder2 = new MsftDiscRecorder2(); discRecorder2.InitializeDiscRecorder(uniqueRecorderId); MsftDiscFormat2Data datawriter = new MsftDiscFormat2Data(); datawriter.Recorder = discRecorder2; datawriter.ClientName = "IMAPIv2 TEST"; MsftFileSystemImage FSI = new MsftFileSystemImage(); try { if (!datawriter.MediaHeuristicallyBlank) { FSI.MultisessionInterfaces = datawriter.MultisessionInterfaces; FSI.ImportFileSystem(); } } catch (Exception) { FSI.ChooseImageDefaults(discRecorder2); Console.WriteLine("Multisession is not supported on this disk!"); } try { FSI.Root.AddTree(path, false); IFileSystemImageResult Result = FSI.CreateResultImage(); var stream = Result.ImageStream; Console.WriteLine("\nWriting to disc now!!"); datawriter.Write(stream); Console.WriteLine("\nWrite Process completed!"); } catch (Exception) { Console.WriteLine("Unable to form image from given path!"); Console.WriteLine("\nAborted process!"); } discRecorder2.EjectMedia(); } #endregion OperationContext.Current.GetCallbackChannel <MyCallBackHandler>().CDBurnt(); }
/// <summary> /// Burns a boot image and data files to disc in a single session /// using files from a single directory tree. /// </summary> /// <param name="recorder">Burning Device. Must be initialized.</param> /// <param name="path">Directory of files to burn. /// \\winbuilds\release\winmain\latest.tst\amd64fre\en-us\skus.cmi\staged\windows /// </param> /// <param name="bootFile">Path and filename of boot image. /// \\winbuilds\release\winmain\latest.tst\x86fre\bin\etfsboot.com /// </param> public void CreateBootDisc(IDiscRecorder2 recorder, String path, String bootFile) { // -------- Adding Boot Image Code ----- Console.WriteLine("Creating BootOptions"); IBootOptions bootOptions = new MsftBootOptions(); bootOptions.Manufacturer = "Microsoft"; bootOptions.PlatformId = PlatformId.PlatformX86; bootOptions.Emulation = EmulationType.EmulationNone; // Need stream for boot image file Console.WriteLine("Creating IStream for file {0}", bootFile); IStream iStream = new AStream( new FileStream(bootFile, FileMode.Open, FileAccess.Read, FileShare.Read)); bootOptions.AssignBootImage(iStream); // Create disc file system image (ISO9660 in this example) IFileSystemImage fsi = new MsftFileSystemImage(); fsi.FreeMediaBlocks = -1; // Enables larger-than-CD image fsi.FileSystemsToCreate = FsiFileSystems.FsiFileSystemISO9660 | FsiFileSystems.FsiFileSystemJoliet | FsiFileSystems.FsiFileSystemUDF; // Hooking bootStream to FileSystemObject fsi.BootImageOptions = bootOptions; // Hooking content files FileSystemObject fsi.Root.AddTree(path, false); IFileSystemImageResult result = fsi.CreateResultImage(); IStream stream = result.ImageStream; // Create and write stream to disc using the specified recorder. IDiscFormat2Data dataWriterBurn = new MsftDiscFormat2Data(); dataWriterBurn.Recorder = recorder; dataWriterBurn.ClientName = "IMAPI Sample"; dataWriterBurn.Write(stream); Console.WriteLine("----- Finished writing content -----"); }
void IDisposable.Dispose() { Monitor.Enter(this); try { if (_fsres != null) { Marshal.FinalReleaseComObject(_fsres); _fsres = null; } } catch (SynchronizationLockException) { } finally { Monitor.Exit(this); GC.SuppressFinalize(this); } }
/// <summary> /// Create the optical disk image. /// </summary> /// <returns></returns> public COMTypes.IStream CreateImageStream(IFileSystemImageResult fsres) { var ppos = IntPtr.Zero; COMTypes.IStream imagestream = null; try { _fsres = fsres; imagestream = fsres.ImageStream; ppos = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(long))); imagestream.Seek(0, 0, ppos); if (Marshal.ReadInt64(ppos) != 0) { throw new IOException("Can't reset the stream position"); } //IDiscFormat2Data idf = this as IDiscFormat2Data; //idf.Write(imagestream); } catch (Exception exc) { if (!string.IsNullOrEmpty(OutputFileName)) { File.Delete(OutputFileName); } Debug.WriteLine(exc.ToString()); } finally { if (ppos != IntPtr.Zero) { Marshal.FreeHGlobal(ppos); } } return(imagestream); }
public ISOFormatter(ImageRepository img) { _fsres = ((IFileSystemImage)img).CreateResultImage(); }
public void CreateISO() { Extraction.WriteResource(Resources.BIOS, Directories.TempPath + "Files\\BIOS.com"); _repository.BootableImageFile = Directories.TempPath + "Files\\BIOS.com"; Application.DoEvents(); OnPropertyChanged("0|Enumerating files..."); var directory = new DirectoryInfo(_path); var files = directory.GetFiles("*", SearchOption.AllDirectories); var filtered = files.Select(f => f) .Where(f => (f.Attributes & FileAttributes.Directory) != FileAttributes.Directory); foreach (var file in filtered) { _repository.AddNewFile(file.FullName); } IFileSystemImageResult imageResult = null; COMTypes.IStream imagestream = null; try { var ifsi = InitRepository(); Application.DoEvents(); ifsi.CaptureDirectory = _path; imageResult = ifsi.CreateResultImage(); if (imageResult == null) { } formatter = new ISOFormatter(_output); DiscFormat2Data_Events ev = formatter; // if (_ckUseUIReport.Checked) ev.Update += FormattingEvent; imagestream = formatter.CreateImageStream(imageResult); IDiscFormat2Data idf = formatter; try { idf.Write(imagestream); } catch (ApplicationException) { throw; } catch (IOException) { //WaitForSelection(_output); //if (_backgroundISOWorker.CancellationPending) // throw; idf.Write(imagestream); } catch (COMException ex) { if (ex.ErrorCode == -2147024864) { //WaitForSelection(_output); //if (_backgroundISOWorker.CancellationPending) // throw; idf.Write(imagestream); } else { throw; } } } catch (COMException ex) { //Console.Beep(); //if (ex.ErrorCode == -1062555360) //{ // _lblUpdate.Text = "On UI Thread: Media size could be too small for the amount of data"; //} //else // _lblUpdate.Text = "On UI Thread: " + ex.Message; if (ex.ErrorCode != -2147024864 && File.Exists(_output)) { File.Delete(_output); } } catch (Exception ex) { //if (!this.IsDisposed) //{ // if (_repository.Cancel) // _lblUpdate.Text = "Canceled on UI thread"; // else // { // Console.Beep(); // _lblUpdate.Text = "Failed on UI thread: " + ex.Message; // } //} if (!(ex is IOException) && File.Exists(_output)) { File.Delete(_output); } } finally { if (imagestream != null) { Marshal.FinalReleaseComObject(imagestream); imagestream = null; } if (_repository.Cancel && !string.IsNullOrEmpty(_output)) { File.Delete(_output); } //else if (!_ckWorker.Checked) // _lblFileImage.Text = _output; //if (!_ckWorker.Checked) // RestoreUI(formatter); FileHandling.DeleteFile(Directories.TempPath + "Files\\BIOS.com"); } }