IMAPI:如果图像大小超过免费 space,如何在不抛出异常的情况下获取图像大小?

IMAPI: How to get image size without throwing exception if image size exceeds free space?

我正在编写代码以使用 IMAPI (C#.NET) 编写媒体 (CD/DVD)。 写作一切正常。 如果我尝试在媒体上添加超过可用 space 的文件,它会抛出异常。 我可以捕获异常,但它不符合我的目的。 我想添加文件,即使它超过了免费 space。 然后,我想 return 图像的总大小给用户,然后用户将根据要求进行必要的操作(更新 UI、显示消息、在 UI 上显示图像大小等)。 换句话说,我想获得总图像大小,即使它超过了免费 space。 我可以看到很多媒体刻录应用程序都在这样做;所以这一定是可能的。

代码中的下一行抛出异常:-

fileSystemImage.Root.AddFile(thisFileItem.DestPath + thisFileItem.DisplayName, stream);

异常详情如下:-

ComException

ErrorCode: -1062555360

Message: Adding 'myfilename' would result in a result image having a size larger than the current configured limit.

以下是我的代码:-

MsftDiscFormat2Data discFormatData = null;
MsftDiscRecorder2 discRecorder = null;
MsftFileSystemImage fileSystemImage = null;

discRecorder = new MsftDiscRecorder2();
discRecorder.InitializeDiscRecorder(UniqueRecorderId);

discFormatData = new MsftDiscFormat2Data
{
    Recorder = discRecorder,
    ClientName = CLIENT_NAME,
    ForceMediaToBeClosed = _closeSession
};

fileSystemImage = new MsftFileSystemImage();
fileSystemImage.VolumeName = _volumeID;
fileSystemImage.Update += fileSystemImage_Update;
fileSystemImage.ChooseImageDefaults(discRecorder);

fileSystemImage.FileSystemsToCreate = FsiFileSystems.FsiFileSystemJoliet | FsiFileSystems.FsiFileSystemISO9660 | FsiFileSystems.FsiFileSystemUDF;

if(!discFormatData.MediaHeuristicallyBlank)
{
    fileSystemImage.MultisessionInterfaces = discFormatData.MultisessionInterfaces;
    fileSystemImage.ImportFileSystem();
}

foreach(FileItem thisFileItem in listFileItem)
{
    IStream stream = null;
    if(thisFileItem.SourcePath != null)
        Win32.SHCreateStreamOnFile(thisFileItem.SourcePath, Win32.STGM_READ | Win32.STGM_SHARE_DENY_WRITE, ref stream);
    fileSystemImage.Root.AddFile(thisFileItem.DestPath + thisFileItem.DisplayName, stream);
}

IFileSystemImageResult objIFileSystemImageResult;
objIFileSystemImageResult = fileSystemImage.CreateResultImage();
_imageSize = (objIFileSystemImageResult.TotalBlocks * objIFileSystemImageResult.BlockSize);
IStream imageStream = objIFileSystemImageResult.ImageStream;
fileSystemImage.Update -= fileSystemImage_Update;

不是解决方案,但我找到了窍门。

fileSystemImage.FreeMediaBlocks = int.MaxValue;

这将允许创建任意大小(最大整数限制)的图像,而不管插入的媒体上有多少空闲块。然后,您可以按照自己的方式实施验证。