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

586 Views Asked by At

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;
1

There are 1 best solutions below

0
On BEST ANSWER

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.