IMAPI: How to get image size without throwing exce

2019-09-16 06:18发布

I am writing a code to write a media (CD/DVD) using IMAPI (C#.NET). The writing works all fine. In case when I try to add file which exceeds the free space on media, it throws exception. I can catch the exception but it does not serve my purpose. I want to go adding the files even though it exceeds the free space. Then, I want to return total size of image to user who will then do the needful (update UI, show message, show size of image on UI etc.) as per requirements. In other words, I want to get total image size even though it exceeds the free space. I can see lot many media burning applications are doing this; so this must be possible.

Following line in code throws exception: -

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

Following are the exception details: -

ComException

ErrorCode: -1062555360

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

Following is my code: -

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;

标签: c# imapi
1条回答
小情绪 Triste *
2楼-- · 2019-09-16 07:12

Not a solution, but i got the hack.

fileSystemImage.FreeMediaBlocks = int.MaxValue;

This will allow to create image of any size (upto integer limit) irrespective of free blocks on media inserted. You can then implement validations in your own way.

查看更多
登录 后发表回答