C#挪用Nero SDK刻錄光盤的辦法。本站提示廣大學習愛好者:(C#挪用Nero SDK刻錄光盤的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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#法式設計有所贊助。