Port New-IsoFile to C# .Net code RRS feed

  • Question

  • Hi, 

    I am having trouble porting the code of Chris Wu to C# .net. I love the function New-IsoFile ( to build some bootable iso files within powershell. But now I am trying to port this function to C# so I can use It in my C# projects. But for some reason I get stuck.  Can anybody help me to point me in the right direction. My code is as follow.

    I have referenced the ADODB from nugets. the IMAPI2FS.DLL  from c:\windows\system32.   The error i got is that the file could't not be found, when i am in debug mode. ( It fails when I create a new object from the constructor, the error appears at the end of the }.

    using System;

    using IMAPI2FS;

    using ADODB;

    using ComTypes = System.Runtime.InteropServices.ComTypes;

    namespace IsoBuilder_Test


        class Iso


            ADODB.Stream _stream;

            BootOptions _boot;

            MsftFileSystemImage _image;

            IFileSystemImageResult _result;

            private Iso() { }


            public Iso(string title)


                _stream = new ADODB.Stream();

                _stream.Type = StreamTypeEnum.adTypeBinary;



                var fsiStream = (FsiStream)_stream;

                _boot = new BootOptions();


                _image = new MsftFileSystemImage() { VolumeName = title };



                _image.BootImageOptions = _boot;

               _result = _image.CreateResultImage();


            public unsafe void Create(string Path)


                var totalBlocks = _result.TotalBlocks;

                using (var o = System.IO.File.OpenWrite(Path))


                    int bytes = 0;

                    byte[] buf = new byte[_result.BlockSize];

                    var ptr = (IntPtr)(&bytes);

                    var i = _result.ImageStream as ComTypes.IStream;



                        i.Read(buf, _result.BlockSize, ptr); o.Write(buf, 0, bytes);

                    } while (totalBlocks-- > 0);





    Wednesday, August 14, 2019 7:56 PM

All replies

  • Ask in (Q & A)

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Wednesday, August 14, 2019 8:56 PM
  • Hi kevin,

    Thank you for posting here.

    Based on your description, you want to convert powershell code to c#.

    I am not good at powershell. Therefore, I suggest that you could use the following link to call powershell functions from c#.

    Calling Powershell functions from C#

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, August 15, 2019 5:33 AM
  • Hi Jack,

    Thank you for your reply,that it is possible to call powershell functions from C#. That will indeed run the code and is a solution, but what i want is to port it back to native C# and calling com objects. I thought I did the calling right, with the using namespaces and the unsafe code, but somehow, It sayw It can't find the IMAPI2FS part, while i have referenced to it. Some how i gut stuck on that part, is there some other IMAPI2FS that i have to use in C# then the one in system32 ?

    • Edited by kevin sail Friday, August 16, 2019 6:39 PM
    Friday, August 16, 2019 6:38 PM
  • Thanks,

    I have missed that part of Q & A, I will ask my question there aswell.

    Friday, August 16, 2019 8:11 PM