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