C#调用Nero SDK刻录光盘的方法
程序员文章站
2023-11-20 22:15:22
本文实例讲述了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#程序设计有所帮助。