| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175 |
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Runtime.InteropServices.ComTypes;
- using System.Text;
- using System.Windows.Forms;
- using IMAPI2.Interop;
- using IMAPI2.MediaItem;
- namespace BurnMedia
- {
- public partial class MainForm : Form
- {
- private const string ClientName = "BurnMedia";
- Int64 _totalDiscSize;
- private bool _isBurning;
- private bool _isFormatting;
- private IMAPI_BURN_VERIFICATION_LEVEL _verificationLevel =
- IMAPI_BURN_VERIFICATION_LEVEL.IMAPI_BURN_VERIFICATION_NONE;
- private bool _closeMedia;
- private bool _ejectMedia;
- private BurnData _burnData = new BurnData();
- public MainForm()
- {
- InitializeComponent();
- }
- /// <summary>
- /// Initialize the form
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MainForm_Load(object sender, EventArgs e)
- {
- //
- // Determine the current recording devices
- //
- MsftDiscMaster2 discMaster = null;
- try
- {
- discMaster = new MsftDiscMaster2();
- if (!discMaster.IsSupportedEnvironment)
- return;
- foreach (string uniqueRecorderId in discMaster)
- {
- var discRecorder2 = new MsftDiscRecorder2();
- discRecorder2.InitializeDiscRecorder(uniqueRecorderId);
- devicesComboBox.Items.Add(discRecorder2);
- }
- if (devicesComboBox.Items.Count > 0)
- {
- devicesComboBox.SelectedIndex = 0;
- }
- }
- catch (COMException ex)
- {
- MessageBox.Show(ex.Message,
- string.Format("Error:{0} - Please install IMAPI2", ex.ErrorCode),
- MessageBoxButtons.OK, MessageBoxIcon.Stop);
- return;
- }
- finally
- {
- if (discMaster != null)
- {
- Marshal.ReleaseComObject(discMaster);
- }
- }
- //
- // Create the volume label based on the current date
- //
- DateTime now = DateTime.Now;
- textBoxLabel.Text = now.Year + "_" + now.Month + "_" + now.Day;
- labelStatusText.Text = string.Empty;
- labelFormatStatusText.Text = string.Empty;
- //
- // Select no verification, by default
- //
- comboBoxVerification.SelectedIndex = 0;
- UpdateCapacity();
- }
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- //
- // Release the disc recorder items
- //
- foreach (MsftDiscRecorder2 discRecorder2 in devicesComboBox.Items)
- {
- if (discRecorder2 != null)
- {
- Marshal.ReleaseComObject(discRecorder2);
- }
- }
- }
- #region Device ComboBox
- /// <summary>
- /// Selected a new device
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void devicesComboBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (devicesComboBox.SelectedIndex == -1)
- {
- return;
- }
- var discRecorder =
- (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex];
- supportedMediaLabel.Text = string.Empty;
- //
- // Verify recorder is supported
- //
- IDiscFormat2Data discFormatData = null;
- try
- {
- discFormatData = new MsftDiscFormat2Data();
- if (!discFormatData.IsRecorderSupported(discRecorder))
- {
- MessageBox.Show("Recorder not supported", ClientName,
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- StringBuilder supportedMediaTypes = new StringBuilder();
- foreach (IMAPI_PROFILE_TYPE profileType in discRecorder.SupportedProfiles)
- {
- string profileName = GetProfileTypeString(profileType);
- if (string.IsNullOrEmpty(profileName))
- continue;
- if (supportedMediaTypes.Length > 0)
- supportedMediaTypes.Append(", ");
- supportedMediaTypes.Append(profileName);
- }
- supportedMediaLabel.Text = supportedMediaTypes.ToString();
- }
- catch (COMException)
- {
- supportedMediaLabel.Text = "Error getting supported types";
- }
- finally
- {
- if (discFormatData != null)
- {
- Marshal.ReleaseComObject(discFormatData);
- }
- }
- }
- /// <summary>
- /// converts an IMAPI_MEDIA_PHYSICAL_TYPE to it's string
- /// </summary>
- /// <param name="mediaType"></param>
- /// <returns></returns>
- private static string GetMediaTypeString(IMAPI_MEDIA_PHYSICAL_TYPE mediaType)
- {
- switch (mediaType)
- {
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_UNKNOWN:
- default:
- return "Unknown Media Type";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_CDROM:
- return "CD-ROM";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_CDR:
- return "CD-R";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_CDRW:
- return "CD-RW";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDROM:
- return "DVD ROM";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDRAM:
- return "DVD-RAM";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDPLUSR:
- return "DVD+R";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDPLUSRW:
- return "DVD+RW";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDPLUSR_DUALLAYER:
- return "DVD+R Dual Layer";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDDASHR:
- return "DVD-R";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDDASHRW:
- return "DVD-RW";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDDASHR_DUALLAYER:
- return "DVD-R Dual Layer";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DISK:
- return "random-access writes";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDPLUSRW_DUALLAYER:
- return "DVD+RW DL";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_HDDVDROM:
- return "HD DVD-ROM";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_HDDVDR:
- return "HD DVD-R";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_HDDVDRAM:
- return "HD DVD-RAM";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_BDROM:
- return "Blu-ray DVD (BD-ROM)";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_BDR:
- return "Blu-ray media";
- case IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_BDRE:
- return "Blu-ray Rewritable media";
- }
- }
- /// <summary>
- /// converts an IMAPI_PROFILE_TYPE to it's string
- /// </summary>
- /// <param name="profileType"></param>
- /// <returns></returns>
- static string GetProfileTypeString(IMAPI_PROFILE_TYPE profileType)
- {
- switch (profileType)
- {
- default:
- return string.Empty;
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_CD_RECORDABLE:
- return "CD-R";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_CD_REWRITABLE:
- return "CD-RW";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVDROM:
- return "DVD ROM";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_DASH_RECORDABLE:
- return "DVD-R";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_RAM:
- return "DVD-RAM";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_PLUS_R:
- return "DVD+R";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_PLUS_RW:
- return "DVD+RW";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_PLUS_R_DUAL:
- return "DVD+R Dual Layer";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_DASH_REWRITABLE:
- return "DVD-RW";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_DASH_RW_SEQUENTIAL:
- return "DVD-RW Sequential";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_DASH_R_DUAL_SEQUENTIAL:
- return "DVD-R DL Sequential";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_DASH_R_DUAL_LAYER_JUMP:
- return "DVD-R Dual Layer";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_DVD_PLUS_RW_DUAL:
- return "DVD+RW DL";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_HD_DVD_ROM:
- return "HD DVD-ROM";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_HD_DVD_RECORDABLE:
- return "HD DVD-R";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_HD_DVD_RAM:
- return "HD DVD-RAM";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_BD_ROM:
- return "Blu-ray DVD (BD-ROM)";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_BD_R_SEQUENTIAL:
- return "Blu-ray media Sequential";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_BD_R_RANDOM_RECORDING:
- return "Blu-ray media";
- case IMAPI_PROFILE_TYPE.IMAPI_PROFILE_TYPE_BD_REWRITABLE:
- return "Blu-ray Rewritable media";
- }
- }
- /// <summary>
- /// Provides the display string for an IDiscRecorder2 object in the combobox
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void devicesComboBox_Format(object sender, ListControlConvertEventArgs e)
- {
- IDiscRecorder2 discRecorder2 = (IDiscRecorder2)e.ListItem;
- string devicePaths = string.Empty;
- string volumePath = (string)discRecorder2.VolumePathNames.GetValue(0);
- foreach (string volPath in discRecorder2.VolumePathNames)
- {
- if (!string.IsNullOrEmpty(devicePaths))
- {
- devicePaths += ",";
- }
- devicePaths += volumePath;
- }
- e.Value = string.Format("{0} [{1}]", devicePaths, discRecorder2.ProductId);
- }
- #endregion
- #region Media Size
- private void buttonDetectMedia_Click(object sender, EventArgs e)
- {
- if (devicesComboBox.SelectedIndex == -1)
- {
- return;
- }
- var discRecorder =
- (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex];
- MsftFileSystemImage fileSystemImage = null;
- MsftDiscFormat2Data discFormatData = null;
- try
- {
- //
- // Create and initialize the IDiscFormat2Data
- //
- discFormatData = new MsftDiscFormat2Data();
- if (!discFormatData.IsCurrentMediaSupported(discRecorder))
- {
- labelMediaType.Text = "Media not supported!";
- _totalDiscSize = 0;
- return;
- }
- else
- {
- //
- // Get the media type in the recorder
- //
- discFormatData.Recorder = discRecorder;
- IMAPI_MEDIA_PHYSICAL_TYPE mediaType = discFormatData.CurrentPhysicalMediaType;
- labelMediaType.Text = GetMediaTypeString(mediaType);
- //
- // Create a file system and select the media type
- //
- fileSystemImage = new MsftFileSystemImage();
- fileSystemImage.ChooseImageDefaultsForMediaType(mediaType);
- //
- // See if there are other recorded sessions on the disc
- //
- if (!discFormatData.MediaHeuristicallyBlank)
- {
- fileSystemImage.MultisessionInterfaces = discFormatData.MultisessionInterfaces;
- fileSystemImage.ImportFileSystem();
- }
- Int64 freeMediaBlocks = fileSystemImage.FreeMediaBlocks;
- _totalDiscSize = 2048 * freeMediaBlocks;
- }
- }
- catch (COMException exception)
- {
- MessageBox.Show(this, exception.Message, "Detect Media Error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- if (discFormatData != null)
- {
- Marshal.ReleaseComObject(discFormatData);
- }
- if (fileSystemImage != null)
- {
- Marshal.ReleaseComObject(fileSystemImage);
- }
- }
- UpdateCapacity();
- }
- /// <summary>
- /// Updates the capacity progressbar
- /// </summary>
- private void UpdateCapacity()
- {
- //
- // Get the text for the Max Size
- //
- if (_totalDiscSize == 0)
- {
- labelTotalSize.Text = "0MB";
- return;
- }
-
- labelTotalSize.Text = _totalDiscSize < 1000000000 ?
- string.Format("{0}MB", _totalDiscSize / 1000000) :
- string.Format("{0:F2}GB", (float)_totalDiscSize / 1000000000.0);
- //
- // Calculate the size of the files
- //
- Int64 totalMediaSize = 0;
- foreach (IMediaItem mediaItem in listBoxFiles.Items)
- {
- totalMediaSize += mediaItem.SizeOnDisc;
- }
- if (totalMediaSize == 0)
- {
- progressBarCapacity.Value = 0;
- progressBarCapacity.ForeColor = SystemColors.Highlight;
- }
- else
- {
- var percent = (int)((totalMediaSize * 100) / _totalDiscSize);
- if (percent > 100)
- {
- progressBarCapacity.Value = 100;
- progressBarCapacity.ForeColor = Color.Red;
- }
- else
- {
- progressBarCapacity.Value = percent;
- progressBarCapacity.ForeColor = SystemColors.Highlight;
- }
- }
- }
- #endregion
- #region Burn Media Process
- /// <summary>
- /// User clicked the "Burn" button
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void buttonBurn_Click(object sender, EventArgs e)
- {
- if (devicesComboBox.SelectedIndex == -1)
- {
- return;
- }
- if (_isBurning)
- {
- buttonBurn.Enabled = false;
- backgroundBurnWorker.CancelAsync();
- }
- else
- {
- _isBurning = true;
- _closeMedia = checkBoxCloseMedia.Checked;
- _ejectMedia = checkBoxEject.Checked;
- EnableBurnUI(false);
- var discRecorder =
- (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex];
- _burnData.uniqueRecorderId = discRecorder.ActiveDiscRecorder;
- backgroundBurnWorker.RunWorkerAsync(_burnData);
- }
- }
- /// <summary>
- /// The thread that does the burning of the media
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void backgroundBurnWorker_DoWork(object sender, DoWorkEventArgs e)
- {
- MsftDiscRecorder2 discRecorder = null;
- MsftDiscFormat2Data discFormatData = null;
- try
- {
- //
- // Create and initialize the IDiscRecorder2 object
- //
- discRecorder = new MsftDiscRecorder2();
- var burnData = (BurnData)e.Argument;
- discRecorder.InitializeDiscRecorder(burnData.uniqueRecorderId);
- //
- // Create and initialize the IDiscFormat2Data
- //
- discFormatData = new MsftDiscFormat2Data
- {
- Recorder = discRecorder,
- ClientName = ClientName,
- ForceMediaToBeClosed = _closeMedia
- };
- //
- // Set the verification level
- //
- var burnVerification = (IBurnVerification)discFormatData;
- burnVerification.BurnVerificationLevel = _verificationLevel;
- //
- // Check if media is blank, (for RW media)
- //
- object[] multisessionInterfaces = null;
- if (!discFormatData.MediaHeuristicallyBlank)
- {
- multisessionInterfaces = discFormatData.MultisessionInterfaces;
- }
- //
- // Create the file system
- //
- IStream fileSystem;
- if (!CreateMediaFileSystem(discRecorder, multisessionInterfaces, out fileSystem))
- {
- e.Result = -1;
- return;
- }
- //
- // add the Update event handler
- //
- discFormatData.Update += discFormatData_Update;
- //
- // Write the data here
- //
- try
- {
- discFormatData.Write(fileSystem);
- e.Result = 0;
- }
- catch (COMException ex)
- {
- e.Result = ex.ErrorCode;
- MessageBox.Show(ex.Message, "IDiscFormat2Data.Write failed",
- MessageBoxButtons.OK, MessageBoxIcon.Stop);
- }
- finally
- {
- if (fileSystem != null)
- {
- Marshal.FinalReleaseComObject(fileSystem);
- }
- }
- //
- // remove the Update event handler
- //
- discFormatData.Update -= discFormatData_Update;
- if (_ejectMedia)
- {
- discRecorder.EjectMedia();
- }
- }
- catch (COMException exception)
- {
- //
- // If anything happens during the format, show the message
- //
- MessageBox.Show(exception.Message);
- e.Result = exception.ErrorCode;
- }
- finally
- {
- if (discRecorder != null)
- {
- Marshal.ReleaseComObject(discRecorder);
- }
- if (discFormatData != null)
- {
- Marshal.ReleaseComObject(discFormatData);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="progress"></param>
- void discFormatData_Update([In, MarshalAs(UnmanagedType.IDispatch)] object sender, [In, MarshalAs(UnmanagedType.IDispatch)] object progress)
- {
- //
- // Check if we've cancelled
- //
- if (backgroundBurnWorker.CancellationPending)
- {
- var format2Data = (IDiscFormat2Data)sender;
- format2Data.CancelWrite();
- return;
- }
- var eventArgs = (IDiscFormat2DataEventArgs)progress;
- _burnData.task = BURN_MEDIA_TASK.BURN_MEDIA_TASK_WRITING;
- // IDiscFormat2DataEventArgs Interface
- _burnData.elapsedTime = eventArgs.ElapsedTime;
- _burnData.remainingTime = eventArgs.RemainingTime;
- _burnData.totalTime = eventArgs.TotalTime;
- // IWriteEngine2EventArgs Interface
- _burnData.currentAction = eventArgs.CurrentAction;
- _burnData.startLba = eventArgs.StartLba;
- _burnData.sectorCount = eventArgs.SectorCount;
- _burnData.lastReadLba = eventArgs.LastReadLba;
- _burnData.lastWrittenLba = eventArgs.LastWrittenLba;
- _burnData.totalSystemBuffer = eventArgs.TotalSystemBuffer;
- _burnData.usedSystemBuffer = eventArgs.UsedSystemBuffer;
- _burnData.freeSystemBuffer = eventArgs.FreeSystemBuffer;
- //
- // Report back to the UI
- //
- backgroundBurnWorker.ReportProgress(0, _burnData);
- }
- /// <summary>
- /// Completed the "Burn" thread
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void backgroundBurnWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- labelStatusText.Text = (int)e.Result == 0 ? "Finished Burning Disc!" : "Error Burning Disc!";
- statusProgressBar.Value = 0;
- _isBurning = false;
- EnableBurnUI(true);
- buttonBurn.Enabled = true;
- }
- /// <summary>
- /// Enables/Disables the "Burn" User Interface
- /// </summary>
- /// <param name="enable"></param>
- void EnableBurnUI(bool enable)
- {
- buttonBurn.Text = enable ? "&Burn" : "&Cancel";
- buttonDetectMedia.Enabled = enable;
- devicesComboBox.Enabled = enable;
- listBoxFiles.Enabled = enable;
- buttonAddFiles.Enabled = enable;
- buttonAddFolders.Enabled = enable;
- buttonRemoveFiles.Enabled = enable;
- checkBoxEject.Enabled = enable;
- checkBoxCloseMedia.Enabled = enable;
- textBoxLabel.Enabled = enable;
- comboBoxVerification.Enabled = enable;
- }
- /// <summary>
- /// Event receives notification from the Burn thread of an event
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void backgroundBurnWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- //int percent = e.ProgressPercentage;
- var burnData = (BurnData)e.UserState;
- if (burnData.task == BURN_MEDIA_TASK.BURN_MEDIA_TASK_FILE_SYSTEM)
- {
- labelStatusText.Text = burnData.statusMessage;
- }
- else if (burnData.task == BURN_MEDIA_TASK.BURN_MEDIA_TASK_WRITING)
- {
- switch (burnData.currentAction)
- {
- case IMAPI_FORMAT2_DATA_WRITE_ACTION.IMAPI_FORMAT2_DATA_WRITE_ACTION_VALIDATING_MEDIA:
- labelStatusText.Text = "Validating current media...";
- break;
- case IMAPI_FORMAT2_DATA_WRITE_ACTION.IMAPI_FORMAT2_DATA_WRITE_ACTION_FORMATTING_MEDIA:
- labelStatusText.Text = "Formatting media...";
- break;
- case IMAPI_FORMAT2_DATA_WRITE_ACTION.IMAPI_FORMAT2_DATA_WRITE_ACTION_INITIALIZING_HARDWARE:
- labelStatusText.Text = "Initializing hardware...";
- break;
- case IMAPI_FORMAT2_DATA_WRITE_ACTION.IMAPI_FORMAT2_DATA_WRITE_ACTION_CALIBRATING_POWER:
- labelStatusText.Text = "Optimizing laser intensity...";
- break;
- case IMAPI_FORMAT2_DATA_WRITE_ACTION.IMAPI_FORMAT2_DATA_WRITE_ACTION_WRITING_DATA:
- long writtenSectors = burnData.lastWrittenLba - burnData.startLba;
- if (writtenSectors > 0 && burnData.sectorCount > 0)
- {
- var percent = (int)((100 * writtenSectors) / burnData.sectorCount);
- labelStatusText.Text = string.Format("Progress: {0}%", percent);
- statusProgressBar.Value = percent;
- }
- else
- {
- labelStatusText.Text = "Progress 0%";
- statusProgressBar.Value = 0;
- }
- break;
- case IMAPI_FORMAT2_DATA_WRITE_ACTION.IMAPI_FORMAT2_DATA_WRITE_ACTION_FINALIZATION:
- labelStatusText.Text = "Finalizing writing...";
- break;
- case IMAPI_FORMAT2_DATA_WRITE_ACTION.IMAPI_FORMAT2_DATA_WRITE_ACTION_COMPLETED:
- labelStatusText.Text = "Completed!";
- break;
- case IMAPI_FORMAT2_DATA_WRITE_ACTION.IMAPI_FORMAT2_DATA_WRITE_ACTION_VERIFYING:
- labelStatusText.Text = "Verifying";
- break;
- }
- }
- }
- /// <summary>
- /// Enable the Burn Button if items in the file listbox
- /// </summary>
- private void EnableBurnButton()
- {
- buttonBurn.Enabled = (listBoxFiles.Items.Count > 0);
- }
- #endregion
- #region File System Process
- private bool CreateMediaFileSystem(IDiscRecorder2 discRecorder, object[] multisessionInterfaces, out IStream dataStream)
- {
- MsftFileSystemImage fileSystemImage = null;
- try
- {
- fileSystemImage = new MsftFileSystemImage();
- fileSystemImage.ChooseImageDefaults(discRecorder);
- fileSystemImage.FileSystemsToCreate =
- FsiFileSystems.FsiFileSystemJoliet | FsiFileSystems.FsiFileSystemISO9660;
- fileSystemImage.VolumeName = textBoxLabel.Text;
- fileSystemImage.Update += fileSystemImage_Update;
- //
- // If multisessions, then import previous sessions
- //
- if (multisessionInterfaces != null)
- {
- fileSystemImage.MultisessionInterfaces = multisessionInterfaces;
- fileSystemImage.ImportFileSystem();
- }
- //
- // Get the image root
- //
- IFsiDirectoryItem rootItem = fileSystemImage.Root;
- //
- // Add Files and Directories to File System Image
- //
- foreach (IMediaItem mediaItem in listBoxFiles.Items)
- {
- //
- // Check if we've cancelled
- //
- if (backgroundBurnWorker.CancellationPending)
- {
- break;
- }
- //
- // Add to File System
- //
- mediaItem.AddToFileSystem(rootItem);
- }
- fileSystemImage.Update -= fileSystemImage_Update;
- //
- // did we cancel?
- //
- if (backgroundBurnWorker.CancellationPending)
- {
- dataStream = null;
- return false;
- }
- dataStream = fileSystemImage.CreateResultImage().ImageStream;
- }
- catch (COMException exception)
- {
- MessageBox.Show(this, exception.Message, "Create File System Error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- dataStream = null;
- return false;
- }
- finally
- {
- if (fileSystemImage != null)
- {
- Marshal.ReleaseComObject(fileSystemImage);
- }
- }
- return true;
- }
- /// <summary>
- /// Event Handler for File System Progress Updates
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="currentFile"></param>
- /// <param name="copiedSectors"></param>
- /// <param name="totalSectors"></param>
- void fileSystemImage_Update([In, MarshalAs(UnmanagedType.IDispatch)] object sender,
- [In, MarshalAs(UnmanagedType.BStr)]string currentFile, [In] int copiedSectors, [In] int totalSectors)
- {
- var percentProgress = 0;
- if (copiedSectors > 0 && totalSectors > 0)
- {
- percentProgress = (copiedSectors * 100) / totalSectors;
- }
- if (!string.IsNullOrEmpty(currentFile))
- {
- var fileInfo = new FileInfo(currentFile);
- _burnData.statusMessage = "Adding \"" + fileInfo.Name + "\" to image...";
- //
- // report back to the ui
- //
- _burnData.task = BURN_MEDIA_TASK.BURN_MEDIA_TASK_FILE_SYSTEM;
- backgroundBurnWorker.ReportProgress(percentProgress, _burnData);
- }
- }
- #endregion
- #region Add/Remove File(s)/Folder(s)
- /// <summary>
- /// Adds a file to the burn list
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void buttonAddFiles_Click(object sender, EventArgs e)
- {
- if (openFileDialog.ShowDialog(this) == DialogResult.OK)
- {
- var fileItem = new FileItem(openFileDialog.FileName);
- listBoxFiles.Items.Add(fileItem);
- UpdateCapacity();
- EnableBurnButton();
- }
- }
- /// <summary>
- /// Adds a folder to the burn list
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void buttonAddFolders_Click(object sender, EventArgs e)
- {
- if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
- {
- var directoryItem = new DirectoryItem(folderBrowserDialog.SelectedPath);
- listBoxFiles.Items.Add(directoryItem);
- UpdateCapacity();
- EnableBurnButton();
- }
- }
- /// <summary>
- /// User wants to remove a file or folder
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void buttonRemoveFiles_Click(object sender, EventArgs e)
- {
- var mediaItem = (IMediaItem)listBoxFiles.SelectedItem;
- if (mediaItem == null)
- return;
- if (MessageBox.Show("Are you sure you want to remove \"" + mediaItem + "\"?",
- "Remove item", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- listBoxFiles.Items.Remove(mediaItem);
- EnableBurnButton();
- UpdateCapacity();
- }
- }
- #endregion
- #region File ListBox Events
- /// <summary>
- /// The user has selected a file or folder
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void listBoxFiles_SelectedIndexChanged(object sender, EventArgs e)
- {
- buttonRemoveFiles.Enabled = (listBoxFiles.SelectedIndex != -1);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void listBoxFiles_DrawItem(object sender, DrawItemEventArgs e)
- {
- var mediaItem = (IMediaItem)listBoxFiles.Items[e.Index];
- if (mediaItem == null)
- {
- return;
- }
- e.DrawBackground();
- if ((e.State & DrawItemState.Focus) != 0)
- {
- e.DrawFocusRectangle();
- }
- if (mediaItem.FileIconImage != null)
- {
- e.Graphics.DrawImage(mediaItem.FileIconImage, new Rectangle(4, e.Bounds.Y + 4, 16, 16));
- }
- var rectF = new RectangleF(e.Bounds.X + 24, e.Bounds.Y,
- e.Bounds.Width - 24, e.Bounds.Height);
- var font = new Font(FontFamily.GenericSansSerif, 11);
- var stringFormat = new StringFormat
- {
- LineAlignment = StringAlignment.Center,
- Alignment = StringAlignment.Near,
- Trimming = StringTrimming.EllipsisCharacter
- };
- e.Graphics.DrawString(mediaItem.ToString(), font, new SolidBrush(e.ForeColor),
- rectF, stringFormat);
- }
- #endregion
- #region Format/Erase the Disc
- /// <summary>
- /// The user has clicked the "Format" button
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void buttonFormat_Click(object sender, EventArgs e)
- {
- if (devicesComboBox.SelectedIndex == -1)
- {
- return;
- }
- _isFormatting = true;
- EnableFormatUI(false);
- var discRecorder =
- (IDiscRecorder2)devicesComboBox.Items[devicesComboBox.SelectedIndex];
- backgroundFormatWorker.RunWorkerAsync(discRecorder.ActiveDiscRecorder);
- }
- /// <summary>
- /// Enables/Disables the "Burn" User Interface
- /// </summary>
- /// <param name="enable"></param>
- void EnableFormatUI(bool enable)
- {
- buttonFormat.Enabled = enable;
- checkBoxEjectFormat.Enabled = enable;
- checkBoxQuickFormat.Enabled = enable;
- }
- /// <summary>
- /// Worker thread that Formats the Disc
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void backgroundFormatWorker_DoWork(object sender, DoWorkEventArgs e)
- {
- MsftDiscRecorder2 discRecorder = null;
- MsftDiscFormat2Erase discFormatErase = null;
- try
- {
- //
- // Create and initialize the IDiscRecorder2
- //
- discRecorder = new MsftDiscRecorder2();
- var activeDiscRecorder = (string)e.Argument;
- discRecorder.InitializeDiscRecorder(activeDiscRecorder);
- //
- // Create the IDiscFormat2Erase and set properties
- //
- discFormatErase = new MsftDiscFormat2Erase
- {
- Recorder = discRecorder,
- ClientName = ClientName,
- FullErase = !checkBoxQuickFormat.Checked
- };
- //
- // Setup the Update progress event handler
- //
- discFormatErase.Update += discFormatErase_Update;
- //
- // Erase the media here
- //
- try
- {
- discFormatErase.EraseMedia();
- e.Result = 0;
- }
- catch (COMException ex)
- {
- e.Result = ex.ErrorCode;
- MessageBox.Show(ex.Message, "IDiscFormat2.EraseMedia failed",
- MessageBoxButtons.OK, MessageBoxIcon.Stop);
- }
- //
- // Remove the Update progress event handler
- //
- discFormatErase.Update -= discFormatErase_Update;
- //
- // Eject the media
- //
- if (checkBoxEjectFormat.Checked)
- {
- discRecorder.EjectMedia();
- }
- }
- catch (COMException exception)
- {
- //
- // If anything happens during the format, show the message
- //
- MessageBox.Show(exception.Message);
- }
- finally
- {
- if (discRecorder != null)
- {
- Marshal.ReleaseComObject(discRecorder);
- }
- if (discFormatErase != null)
- {
- Marshal.ReleaseComObject(discFormatErase);
- }
- }
- }
- /// <summary>
- /// Event Handler for the Erase Progress Updates
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="elapsedSeconds"></param>
- /// <param name="estimatedTotalSeconds"></param>
- void discFormatErase_Update([In, MarshalAs(UnmanagedType.IDispatch)] object sender, int elapsedSeconds, int estimatedTotalSeconds)
- {
- var percent = elapsedSeconds * 100 / estimatedTotalSeconds;
- //
- // Report back to the UI
- //
- backgroundFormatWorker.ReportProgress(percent);
- }
- private void backgroundFormatWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- labelFormatStatusText.Text = string.Format("Formatting {0}%...", e.ProgressPercentage);
- formatProgressBar.Value = e.ProgressPercentage;
- }
- private void backgroundFormatWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- labelFormatStatusText.Text = (int)e.Result == 0 ?
- "Finished Formatting Disc!" : "Error Formatting Disc!";
- formatProgressBar.Value = 0;
- _isFormatting = false;
- EnableFormatUI(true);
- }
- #endregion
- /// <summary>
- /// Called when user selects a new tab
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
- {
- //
- // Prevent page from changing if we're burning or formatting.
- //
- if (_isBurning || _isFormatting)
- {
- e.Cancel = true;
- }
- }
- /// <summary>
- /// Get the burn verification level when the user changes the selection
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void comboBoxVerification_SelectedIndexChanged(object sender, EventArgs e)
- {
- _verificationLevel = (IMAPI_BURN_VERIFICATION_LEVEL)comboBoxVerification.SelectedIndex;
- }
- }
- }
|