/// <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.</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."); } }
private void OnLoaded(object sender, RoutedEventArgs rea) { NavigationService.RemoveBackEntry(); NavigationService.RemoveBackEntry(); String driveId = (String)Application.Current.Properties["driveId"]; String sourceFile = (String)Application.Current.Properties["sourceFile"]; int writingSpeed = (int)Application.Current.Properties["writingSpeed"]; bool toBeClosed = (bool)Application.Current.Properties["toBeClosed"]; bool toVerify = (bool)Application.Current.Properties["toBeVerified"]; ThreadStart startCreate = delegate() { IDiscRecorder2 recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(driveId); recorder.DisableMcn(); recorder.AcquireExclusiveAccess(true, "imapi_test"); IDiscFormat2Data dataWriterImage = new MsftDiscFormat2Data(); dataWriterImage.Recorder = recorder; dataWriterImage.ForceMediaToBeClosed = toBeClosed; dataWriterImage.ClientName = "test_imapi"; dataWriterImage.SetWriteSpeed(writingSpeed, false); DiscFormat2Data_Events burnProgress = dataWriterImage as DiscFormat2Data_Events; burnProgress.Update += new DiscFormat2Data_EventsHandler(OnBurnProgress); IStream stream = null; SHCreateStreamOnFile(sourceFile, 0, ref stream); POWER_REQUEST_CONTEXT prc; IntPtr hPower = (IntPtr)null; try{ prc.Version = 0; prc.SimpleReasonString = "imapi_test"; prc.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; hPower = PowerCreateRequest(ref prc); PowerSetRequest(hPower, PowerRequestType.PowerRequestDisplayRequired); } catch { } IBurnVerification verify = dataWriterImage as IBurnVerification; if (toVerify) { verify.BurnVerificationLevel = IMAPI_BURN_VERIFICATION_LEVEL.IMAPI_BURN_VERIFICATION_FULL; } else { verify.BurnVerificationLevel = IMAPI_BURN_VERIFICATION_LEVEL.IMAPI_BURN_VERIFICATION_NONE; } try { dataWriterImage.Write(stream); } catch (System.Runtime.InteropServices.COMException e) { string s = "IMAPI Error: " + Convert.ToString(e.ErrorCode, 16); if ((uint)e.ErrorCode == 0xC0AA0002) //E_IMAPI_REQUEST_CANCELLED { // canceled s += "a"; } else if ((uint)e.ErrorCode == 0xC0AA0404) //E_IMAPI_DF2DATA_STREAM_TOO_LARGE_FOR_CURRENT_MEDIA { // Not enought capacity s = Properties.Resources.Page3_Error_NotEnoughCapacity; System.Windows.Forms.DialogResult messageBoxResult = System.Windows.Forms.MessageBox.Show(s, "IMAPI Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } else { System.Windows.Forms.DialogResult messageBoxResult = System.Windows.Forms.MessageBox.Show(s, "IMAPI Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } //else return; //throw; } catch { //error } burnProgress.Update -= new DiscFormat2Data_EventsHandler(OnBurnProgress); try { recorder.ReleaseExclusiveAccess(); recorder.EnableMcn(); } catch { } if (hPower != (IntPtr)null) { PowerClearRequest(hPower, PowerRequestType.PowerRequestDisplayRequired); CloseHandle(hPower); } Dispatcher.Invoke(new System.EventHandler(OnWritingFinished), this, null); }; const UInt32 SC_CLOSE = 0x0000F060; const UInt32 MF_GRAYED = 0x00000001; WindowInteropHelper wih = new WindowInteropHelper(mainWindow); IntPtr hMenu = GetSystemMenu(wih.Handle, 0); EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED); Thread thread = new Thread(startCreate); thread.IsBackground = true; thread.Start(); }
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"); } }