This article describes the method of C# calling Nero SDK to burn CDs. Share it for your reference. The details are as follows:
There is a requirement for multiple optical drive burning in the project, but multiple burning tasks are started at the same time in a process. The process often reports exceptions, so the burning part is made into a console program separately. Each optical drive burning starts a process to execute the burning task. In this way, even if the burning process error occurs, it will not affect the parent process.
The following is the Nero disc engraving class. The SDK adopts NeroSDK-1.08 version and calls the NeroCOM component. Two dlls must be referenced when calling.
public class NeroBurn { #region properties private NeroDrive m_neroDerive; private Nero m_nero; private bool m_aborted; private string m_burnPath; private bool m_isDoneBurn; private int m_returnCode;//0: Failed; 1: Success; -1: Unknown private string m_discvol; //nero private _INeroDriveEvents_OnDoneBurnEventHandler m_evOnDoneBurn; private _INeroDriveEvents_OnAddLogLineEventHandler m_evOnAddLogLine; private _INeroEvents_OnWaitCDEventHandler m_evOnWaitCD; private _INeroDriveEvents_OnAbortedEventHandler m_evOnAborted; private _INeroDriveEvents_OnProgressEventHandler m_evOnProgress; private _INeroEvents_OnWaitCDDoneEventHandler m_evOnWaitCDDone; #endregion #region constructor /// <summary></summary> /// Constructor /// /// <param name="driverletter">Drive letter /// <param name="burnpath">The path to burn /// <param name="discvol">Disk's volume public NeroBurn(string driverletter,string burnpath,string discvol) { m_burnPath = burnpath; m_nero = new NeroClass(); m_neroDerive = allocDevice(driverletter); m_aborted = false; m_isDoneBurn = false; m_discvol = discvol; } #endregion #region Nero event handling /// <summary></summary> /// Initiate a burn event /// /// <param name="bSubscribe"> private void SubscribeToEvents(bool bSubscribe) { if (bSubscribe) { m_evOnAddLogLine = new _INeroDriveEvents_OnAddLogLineEventHandler(m_drive_OnAddLogLine); m_neroDerive.OnAddLogLine += m_evOnAddLogLine; m_evOnWaitCD = new _INeroEvents_OnWaitCDEventHandler(m_nero_OnWaitCD); m_nero.OnWaitCD += m_evOnWaitCD; m_evOnDoneBurn = new _INeroDriveEvents_OnDoneBurnEventHandler(m_drive_OnDoneBurn); m_neroDerive.OnDoneBurn += m_evOnDoneBurn; m_evOnWaitCDDone = new _INeroEvents_OnWaitCDDoneEventHandler(m_nero_OnWaitCDDone); m_nero.OnWaitCDDone += m_evOnWaitCDDone; m_evOnProgress = new _INeroDriveEvents_OnProgressEventHandler(m_drive_OnProgress); m_neroDerive.OnProgress += m_evOnProgress; } else { m_neroDerive.OnAddLogLine -= m_evOnAddLogLine; m_nero.OnWaitCD -= m_evOnWaitCD; m_nero.OnWaitCDDone -= m_evOnWaitCDDone; m_neroDerive.OnDoneBurn -= m_evOnDoneBurn; m_neroDerive.OnProgress -= m_evOnProgress; } } private void m_drive_OnProgress(ref int ProgressInPercent, ref bool Abort) { // This events gives us an opportunity to show progress // as well as abort if needed. // Abort = m_aborted; ("CD" + m_discvol + " " + () + "% had Burned!!"); //c_TaskPercent.Text = () + "%"; } private void m_nero_OnWaitCDDone() { // When waiting on a disc is done, make sure to // enable us and hide the WaitCD form. // ("a Disc has inserted!"); } /// <summary></summary> ///The burning is completed, no matter the failure is successful! /// /// <param name="StatusCode"> private void m_drive_OnDoneBurn(ref NERO_BURN_ERROR StatusCode) { // When burning is over, make sure to unsubscribe from all // events. // SubscribeToEvents(false); if (StatusCode == NERO_BURN_ERROR.NERO_BURN_OK) { m_returnCode = 1; (m_neroDerive.DriveLetter + "Disc volume labeled" + m_discvol + "The burn successfully!!"); } else { m_returnCode = 0; (m_neroDerive.DriveLetter + "Disc volume labeled" + m_discvol + "Burning failed!!"); } m_isDoneBurn = true; ("Burn Finish!!"); } /// <summary></summary> /// If the optical drive is empty, wait for the optical drive /// /// <param name="WaitCD"> /// <param name="WaitCDLocalizedText"> private void m_nero_OnWaitCD(ref NERO_WAITCD_TYPE WaitCD, ref string WaitCDLocalizedText) { ("Wait CD..."); } /// <summary></summary> /// Write a log /// /// <param name="TextType"> /// <param name="Text"> private void m_drive_OnAddLogLine(ref NERO_TEXT_TYPE TextType, ref string Text) { ( Text); } #endregion #region Apply for equipment /// <summary></summary> /// Apply for equipment /// /// <param name="driverletter"> /// <returns></returns> private NeroDrive allocDevice(string driverletter) { NeroDrives drives = m_nero.GetDrives(NERO_MEDIA_TYPE.NERO_MEDIA_DVD_M); if ( > 0) { foreach (NeroDrive d in drives) { if (().Contains(())) return d; } } return null; } #endregion #region Burn /// <summary></summary> /// Burn /// /// <returns></returns>0: Failed; 1: Success; -1: Unknown error public int Burn() { m_isDoneBurn = false; SubscribeToEvents(true); NeroISOTrack isoTrack = new NeroISOTrackClass(); = (NERO_BURN_OPTIONS)((uint)NERO_BURN_OPTIONS.NERO_BURN_OPTION_CREATE_ISO_FS + (uint)NERO_BURN_OPTIONS.NERO_BURN_OPTION_USE_JOLIET); = m_discvol; ("Begin AddFilesAndFoldersToISOTrack."); AddFilesAndFoldersToISOTrack(ref isoTrack, m_burnPath); ("End AddFilesAndFoldersToISOTrack."); if ( == 0 && == 0) { isoTrack = null; return 0; } NERO_BURN_FLAGS flags = new NERO_BURN_FLAGS(); flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_WRITE; flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_CLOSE_SESSION; flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_BUF_UNDERRUN_PROT; flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_DISABLE_ABORT; flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_DAO; flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_CD_TEXT; flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_DETECT_NON_EMPTY_CDRW; flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_SPEED_IN_KBS; try { if (null == m_neroDerive) { ("m_neroDerive is null!!"); return -1; } int speed = 10000; string BurnSpeed_s = ["BurnSpeed"]; if (!(BurnSpeed_s)) speed = (BurnSpeed_s); m_neroDerive.BurnIsoAudioCD("", "", false, isoTrack, null, null, flags, speed, NERO_MEDIA_TYPE.NERO_MEDIA_DVD_M); //Loop execution, waiting for burning to complete while (true) { if(m_isDoneBurn) //If the burning is complete return m_returnCode; (1000); } } catch (Exception e) { m_aborted = true; ("Burn Error : "+); return 0; } return -1; } #endregion #region Helper Function /// <summary></summary> /// This function adds the files and folders to the supplied NeroISOTrack. /// Add by Cola 2008-4-14 /// /// <param name="isoTrack"> /// <param name="discPath"> private void AddFilesAndFoldersToISOTrack(ref NeroISOTrack isoTrack, string discPath) { string sPath = discPath; // The string should really not be empty... // if (sPath != "") { // If path ends in a backslash, it is a folder. // if (sPath[ - 1] == '//') { NeroFolder folder = ; AddFolderRecursively(ref folder, sPath); } else { // This is a file. Create a new NeroFile // change its properties. // NeroFile file = new NeroFileClass(); = sPath; = (sPath); = (sPath); // In this implementation, specified files are added // to the root of the disc only. // (file); } } } /// <summary></summary> /// This function is used to recursively add the path to the supplied /// parent NeroFolder. Add by Cola 2008-4-14 /// /// <param name="folderParent"> /// <param name="sPath"> private void AddFolderRecursively(ref NeroFolder folderParent, string sPath) { NeroFolder folder = new NeroFolderClass(); (folder); string[] sSplits = (new char[] { '//' }, ); if ((0) >= 2) { string sFolderName = sSplits[(0) - 2]; = sFolderName; string[] sDirectories = (sPath); foreach (string sSubDirPath in sDirectories) { AddFolderRecursively(ref folder, sSubDirPath + "//"); } } string[] sFiles = (sPath); foreach (string sFile in sFiles) { NeroFile file = new NeroFileClass(); = sFile; = (sFile); = (sFile); (file); } } #endregion }
Then, call the method of the NeroBurn class in the Main function, and the parameters of the Main function are as follows:
static int Main(string[] args) { string driverletter; //Drive letter, including ':' number string burnpath; //Burn file directory string discvol; //Clean disc coil label if ( == 3) { driverletter = args[0]; burnpath = args[1]; discvol = args[2]; NeroBurn b = new NeroBurn(driverletter, burnpath, discvol); int ret = (); return ret; } return -1;
I hope this article will be helpful to everyone's C# programming.