C#调用Nero SDK刻录光盘的方法
本文实例讲述了C#调用Nero SDK刻录光盘的方法。分享给大家供大家参考。具体如下:
项目中有多光驱刻录的需求,但在一个进程里同时启动多个刻录任务,进程经常报异常,于是把刻录部分单独做了个控制台程序,每个光驱刻录就启动一个进程去执行刻录任务,这样就算刻录进程出错也不会影响到父进程。
以下是Nero 刻碟的类,SDK采用NeroSDK-1.08版本,调用NeroCOM组件。调用时必须引用Interop.NEROLib.dll 和 Interop.NeroVisionAPI.dll 两个dll。
public class NeroBurn { #region 属性 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:失败;1:成功;-1:未知 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 构造函数 /// <summary></summary> /// 构造函数 /// /// <param name="driverletter">盘符 /// <param name="burnpath">待刻录的文件路径 /// <param name="discvol">盘的卷标 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事件处理 /// <summary></summary> /// 发起刻录事件 /// /// <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; Console.WriteLine("光盘" + m_discvol + " " + ProgressInPercent.ToString() + "% had Burned!!"); //c_TaskPercent.Text = ProgressInPercent.ToString() + "%"; } private void m_nero_OnWaitCDDone() { // When waiting on a disc is done, make sure to // enable us and hide the WaitCD form. // Console.WriteLine("a Disc has inserted!"); } /// <summary></summary> ///刻录完成,不管失败成功! /// /// <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; Console.WriteLine(m_neroDerive.DriveLetter + "盘卷标为 " + m_discvol + " 刻录成功!!"); } else { m_returnCode = 0; Console.WriteLine(m_neroDerive.DriveLetter + "盘卷标为 " + m_discvol + " 刻录失败!!"); } m_isDoneBurn = true; Console.WriteLine("Burn Finish!!"); } /// <summary></summary> /// 如果光驱为空,等待光驱 /// /// <param name="WaitCD"> /// <param name="WaitCDLocalizedText"> private void m_nero_OnWaitCD(ref NERO_WAITCD_TYPE WaitCD, ref string WaitCDLocalizedText) { Console.WriteLine("Wait CD..."); } /// <summary></summary> /// 写日志 /// /// <param name="TextType"> /// <param name="Text"> private void m_drive_OnAddLogLine(ref NERO_TEXT_TYPE TextType, ref string Text) { Console.WriteLine( Text); } #endregion #region 申请设备 /// <summary></summary> /// 申请设备 /// /// <param name="driverletter"> /// <returns></returns> private NeroDrive allocDevice(string driverletter) { NeroDrives drives = m_nero.GetDrives(NERO_MEDIA_TYPE.NERO_MEDIA_DVD_M); if (drives.Count > 0) { foreach (NeroDrive d in drives) { if (driverletter.ToLower().Contains(d.DriveLetter.ToLower())) return d; } } return null; } #endregion #region 刻录 /// <summary></summary> /// 刻录 /// /// <returns></returns>0: 失败;1:成功;-1:未知错误 public int Burn() { m_isDoneBurn = false; SubscribeToEvents(true); NeroISOTrack isoTrack = new NeroISOTrackClass(); isoTrack.BurnOptions = (NERO_BURN_OPTIONS)((uint)NERO_BURN_OPTIONS.NERO_BURN_OPTION_CREATE_ISO_FS + (uint)NERO_BURN_OPTIONS.NERO_BURN_OPTION_USE_JOLIET); isoTrack.Name = m_discvol; Console.WriteLine("Begin AddFilesAndFoldersToISOTrack."); AddFilesAndFoldersToISOTrack(ref isoTrack, m_burnPath); Console.WriteLine("End AddFilesAndFoldersToISOTrack."); if (isoTrack.RootFolder.Files.Count == 0 && isoTrack.RootFolder.Folders.Count == 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) { Console.WriteLine("m_neroDerive is null!!"); return -1; } int speed = 10000; string BurnSpeed_s = System.Configuration.ConfigurationSettings.AppSettings["BurnSpeed"]; if (!string.IsNullOrEmpty(BurnSpeed_s)) speed = int.Parse(BurnSpeed_s); m_neroDerive.BurnIsoAudioCD("", "", false, isoTrack, null, null, flags, speed, NERO_MEDIA_TYPE.NERO_MEDIA_DVD_M); //循环执行,等待刻录完成 while (true) { if(m_isDoneBurn) //如果刻录完成 return m_returnCode; System.Threading.Thread.Sleep(1000); } } catch (Exception e) { m_aborted = true; Console.WriteLine("Burn Error : "+e.Message); return 0; } return -1; } #endregion #region 辅助函数 /// <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[sPath.Length - 1] == '//') { NeroFolder folder = isoTrack.RootFolder; AddFolderRecursively(ref folder, sPath); } else { // This is a file. Create a new NeroFile // change its properties. // NeroFile file = new NeroFileClass(); file.SourceFilePath = sPath; file.Name = Path.GetFileName(sPath); file.EntryTime = Directory.GetLastWriteTime(sPath); // In this implementation, specified files are added // to the root of the disc only. // isoTrack.RootFolder.Files.Add(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(); folderParent.Folders.Add(folder); string[] sSplits = sPath.Split(new char[] { '//' }, sPath.Length); if (sSplits.GetLength(0) >= 2) { string sFolderName = sSplits[sSplits.GetLength(0) - 2]; folder.Name = sFolderName; string[] sDirectories = Directory.GetDirectories(sPath); foreach (string sSubDirPath in sDirectories) { AddFolderRecursively(ref folder, sSubDirPath + "//"); } } string[] sFiles = Directory.GetFiles(sPath); foreach (string sFile in sFiles) { NeroFile file = new NeroFileClass(); file.SourceFilePath = sFile; file.Name = Path.GetFileName(sFile); file.EntryTime = Directory.GetLastWriteTime(sFile); folder.Files.Add(file); } } #endregion }
然后,在Main函数中调用NeroBurn类的方法,Main函数的参数如下:
static int Main(string[] args) { string driverletter; //驱动器盘符,含':'号 string burnpath; //刻录文件目录 string discvol; //光盘卷标 if (args.Length == 3) { driverletter = args[0]; burnpath = args[1]; discvol = args[2]; NeroBurn b = new NeroBurn(driverletter, burnpath, discvol); int ret = b.Burn(); return ret; } return -1;
希望本文所述对大家的C#程序设计有所帮助。
赞 (0)