Adding an Individual File to the ImageĪdding a file involves some multiple IFsiDirectoryItem creations if we want the original path to match the one in the image.Īlso notice the presence of LoadCOMStream that uses SHCreateStreamOnFile to create the file added to the image.įinally, I use the Update event used to track the progress.Ĭopy Code private void CreateFSIFile(FileInfo file, IFsiDirectoryItem diritem) You can notice that you can add only folders not individual files, you cannot gracefully cancel this time intensive process and you also don't get any update on the progress of the operation. Imagestream.CopyTo(newStream, stat.cbSize, inBytes, outBytes) Īs I promised it looks pretty simple, but with simplicity we also cut some corners that would make this solution unfit for a real world UI application. IntPtr outBytes = Marshal.AllocHGlobal(Marshal.SizeOf( typeof( long))) IntPtr inBytes = Marshal.AllocHGlobal(Marshal.SizeOf( typeof( long)))
IStream imagestream = ifsi.CreateResultImage().ImageStream ( " c:\\YourDirToArchive", true) // use a valid folder // this will implement the Write method for the formatter Ifsi.ChooseImageDefaultsForMediaType(IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DISK) įsiFileSystems.FsiFileSystemJoliet | FsiFileSystems.FsiFileSystemISO9660
Using the Interop.cs file provided by Microsoft, add these namespaces:Ĭopy Code IFileSystemImage ifsi = new MsftFileSystemImage() There is no one API call that would do that, but doing so is not rocket science. Fortunately Microsoft provided such a shim file and I was glad to use it. A better approach would be using the attribute based types, so we can hook them up with the right COM binaries at runtime, so we can at least build the solution with no hassle. In order to run or even to build this solution, you would have to have those COM objects registered, in our case IMAPIv2.0 installed. One way to use COM objects is just to add references to them in your managed code project. Unfortunately there is no managed API to use, so we still have to rely on COM Interop to use this functionality, nor is there any way I know of reading an optical file image using Image Mastering API v2.0. Unlike the first version, the new one works with new types of media, and more importantly, for my purpose has some support for creating optical file images. Initially available only on Windows Vista, in 2007 Microsoft introduced IMAPIv2.0 for Windows XP and Windows 2003 server. Hence I tried to create this utility using managed code, so you can reuse it easily if another language is not an option.
There are several free utilities that allow you to create file images, but the code is usually not available or available only in C++ like this CodeProject article or in other scripting languages.