问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;//usingIMAPI2FS;usingIMAPI2.Interop;usingSystem.Runtime.InteropServices.ComTypes;namespaceBurnCD{internalclassRecorder:IRecorder{#regionIRecorder成员///<summary>///刻录机对象///</summary>privateMsftDiscRecorder2m_recorder=null;///<summary>///根据指定的光驱路径,建立光驱对象///</summary>///<paramname="strPath">参数是光驱的标示符</param>internalRecorder(stringstrID){try{m_recorder=newMsftDiscRecorder2();m_recorder.InitializeDiscRecorder(strID);}catch(System.ExceptionEx){thrownewSystem.Exception("根据光驱标示符:"+strID+"建立光驱对象出错");}}///<summary>///不允许通过其他途径建立///</summary>privateRecorder(){}///<summary>///要刻录到这个刻录机中的文件或文件夹///</summary>privateList<IBurnMedia>m_needBurnMedias=newList<IBurnMedia>();///<summary>///要刻录的对象,要想修改要刻录的文件,自己从这里找好了,不再给你提供///其他查询方法///</summary>publicList<IBurnMedia>BurnMediaFileList{get{returnm_needBurnMedias;}}///<summary>///添加刻录文件到这个对象中///</summary>///<returns></returns>publicIBurnMediaAddMediaFile(){//列表中新加一个BurnMedia对象BurnMediaburnMedia=newBurnMedia();m_needBurnMedias.Add(burnMedia);returnburnMedia;}///<summary>///刻录///</summary>///<returns></returns>publicboolBurn(outstringExcep){try{returnDoBurn(outExcep);}catch(System.ExceptionEx){Excep=Ex.Message;returnfalse;}}///<summary>///刻录///</summary>privateboolDoBurn(outstringExcep){//正式的开始刻录MsftDiscFormat2DatamsFormat=newMsftDiscFormat2Data();msFormat.Recorder=m_recorder;msFormat.ClientName="15s";//设置verification等级IBurnVerificationburnVer=(IBurnVerification)msFormat;burnVer.BurnVerificationLevel=IMAPI_BURN_VERIFICATION_LEVEL.IMAPI_BURN_VERIFICATION_FULL;//检查是否是blankobject[]sessions=null;if(false==msFormat.MediaHeuristicallyBlank){sessions=(object[])msFormat.MultisessionInterfaces;}//创建文件流IStreamfileStream=null;if(false==CreateFileStream(refsessions,outfileStream,outExcep)){returnfalse;}//写文件try{msFormat.Write(fileStreamasSystem.Runtime.InteropServices.ComTypes.IStream);Excep="WritefiletoDVDsuccess!";returntrue;}catch(System.ExceptionEx){Excep=Ex.Message;returnfalse;}finally{if(fileStream!=null){Marshal.FinalReleaseComObject(fileStream);}}}///<summary>///创建文件流///</summary>///<paramname="sessions"></param>///<paramname="stream"></param>///<returns></returns>privateboolCreateFileStream(refobject[]sessions,outIStreamstream,outstringExcep){MsftFileSystemImagemsImage;try{//设置相关信息msImage=newMsftFileSystemImage();msImage.ChooseImageDefaults(m_recorder);msImage.FileSystemsToCreate=FsiFileSystems.FsiFileSystemISO9660|FsiFileSystems.FsiFileSystemJoliet;msImage.VolumeName="ABC";//以下为抄的,没看懂if(sessions!=null){msImage.MultisessionInterfaces=sessions;msImage.ImportFileSystem();}IFsiDirectoryItemrootItem=msImage.Root;foreach(BurnMediabminm_needBurnMedias){//依次刻录bm.AddToFileSystem(rootItem);}stream=msImage.CreateResultImage().ImageStream;Excep="Createfilestreamsuccess!";returntrue;}catch(System.ExceptionEx){stream=null;Excep=Ex.Message;returnfalse;}}privatelongm_nDiskSize=0;privatelongm_nUseableSize=0;///<summary>///获取光驱中光盘的大小,单位是字节///</summary>///<returns>如果返回-1,则说明当前光驱中没有光盘或者光盘读取错误</returns>publiclongGetDiskSize(){//首先判断光驱是否可以读盘IDiscFormat2DatamsFormat=newMsftDiscFormat2Data();if(msFormat.IsRecorderSupported(m_recorder)==false){//光驱不支持这种格式光盘,或者没有怎么的return-1;}//以下代码,还没有研究明白msFormat.Recorder=m_recorder;//判断是不是有光盘在里面try{IMAPI_FORMAT2_DATA_MEDIA_STATEst=msFormat.CurrentMediaStatus;}catch(System.Exceptionex){//光盘不存在return-1;}//可用的m_nUseableSize=msFormat.FreeSectorsOnMedia*2048;//总大小m_nDiskSize=(msFormat.TotalSectorsOnMedia)*2048;returnm_nDiskSize;}///<summary>///获取可用的光盘空闲空间///</summary>///<returns></returns>publiclongGetDiskUseableSize(){GetDiskSize();returnm_nUseableSize;}#endregion}}然后我写了一个接口实现刻录文件的功能:publicintBurnCD(stringRecordPartition,stringRecordFilePath,outstringExcep){IBurnCDBcd=IBurnCD.CreateInstance();List<string>StrRecorder=Bcd.GetRecorderList();stringPartitionName=StrRecorder.Find(delegate(stringPartition){returnPartition.Equals(RecordPartition);});if(String.IsNullOrEmpty(PartitionName)){Excep="Specifiedpartitionisnotexist!";return1;}if(!File.Exists(RecordFilePath)){Excep="Thefileisnotexist!";return2;}IRecorderRecord=Bcd.GetRecorder(RecordPartition);IBurnMediaBurnMedia=Record.AddMediaFile();BurnMedia.Path=RecordFilePath;BurnMedia.Type=MediaType.File;boolbRet=Record.Burn(outExcep);if(bRet)return0;elsereturn3;}上面的代码在刻录大概800M以下的文件没有问题,但是在刻录800M以上的文件的时候就会抛异常,提示“调用COM组件时失败”请哪位大侠指点?
解决方案
解决方案二:
上面的程序在msFormat.Write(fileStreamasSystem.Runtime.InteropServices.ComTypes.IStream);这条语句执行的时候抛异常了,调用COM组件失败,但是写小文件的时候不会抛异常,返回是成功的。请高手指点原因,不胜感激!高分送上!