C#调用Nero SDK刻录光盘的方法

 更新时间:2015年07月22日 09:53:10   作者:冰封的心  
这篇文章主要介绍了C#调用Nero SDK刻录光盘的方法,涉及C#调用NeroCOM组件实现光盘刻录的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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#程序设计有所帮助。

相关文章

  • 关于nancy中的身份验证

    关于nancy中的身份验证

    在nancy中,身份验证分为basic ,form ,token,stateless几种,basic和form这里不说了,本文重点介绍token验证,需要的朋友一起看看吧
    2015-09-09
  • vs 中C#项目读取JSON配置文件的方法

    vs 中C#项目读取JSON配置文件的方法

    这篇文章主要介绍了vs中 C#项目读取JSON配置文件的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Unity排行榜优化滚动效果

    Unity排行榜优化滚动效果

    这篇文章主要为大家详细介绍了Unity排行榜优化滚动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • C#调用python文件执行

    C#调用python文件执行

    这篇文章主要为大家详细介绍了C#调用python文件执行的相关方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • C#如何使用Bogus创建模拟数据示例代码

    C#如何使用Bogus创建模拟数据示例代码

    这篇文章主要给大家介绍了关于C#如何使用Bogus创建模拟数据的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • c# String扩展 让你在PadLeft和PadRight时不再受单双字节问题困扰

    c# String扩展 让你在PadLeft和PadRight时不再受单双字节问题困扰

    这篇文章主要介绍了c# String扩展 让你在PadLeft和PadRight时不再受单双字节问题困扰,需要的朋友可以参考下
    2020-04-04
  • C#时间戳基本使用方法详解

    C#时间戳基本使用方法详解

    这篇文章主要给大家介绍了关于C#时间戳基本使用方法的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友可以参考下
    2022-10-10
  • 教你创建一个带诊断工具的.NET镜像

    教你创建一个带诊断工具的.NET镜像

    本文编写的初衷是因为在群里有很多小伙伴遇到生产环境性能问题的时候,.NET的runtime镜像中没有带一些工具,安装和使用起来很麻烦,所以分享一些我们公司内部一些技巧,对.NET镜像带诊断工具相关知识感兴趣的朋友一起看看吧
    2022-07-07
  • C#图像灰度级拉伸的方法

    C#图像灰度级拉伸的方法

    这篇文章主要介绍了C#图像灰度级拉伸的方法,涉及C#灰度操作的相关技巧,需要的朋友可以参考下
    2015-04-04
  • C#实现QQ窗口抖动效果

    C#实现QQ窗口抖动效果

    这篇文章主要为大家详细介绍了C#实现QQ窗口抖动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11

最新评论