public void WriteToFile(string FileName, bool Overwrite = true) { IFileSystemImageResult Res = ISO.CreateResultImage(); IStream ImgStream = (IStream)Res.ImageStream; if (ImgStream != null) { STATSTG Stat; ImgStream.Stat(out Stat, 0x1); if (File.Exists(FileName)) { if (Overwrite) { File.Delete(FileName); } else { throw new Exception("File already exists: " + FileName); } } IStream OutStream; SHCreateStreamOnFile(FileName, 0x1001, out OutStream); ImgStream.CopyTo(OutStream, Stat.cbSize, IntPtr.Zero, IntPtr.Zero); OutStream.Commit(0); } }
internal void CreateISOFile(COMTypes.IStream imagestream) { if (Cancel) { return; } COMTypes.IStream newStream; newStream = CreateCOMStreamFromFile(OutputFileName); COMTypes.STATSTG stat; #if DEBUG var tm = new Stopwatch(); tm.Start(); #endif imagestream.Stat(out stat, 0x0); var inBytes = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ulong))); var outBytes = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ulong))); try { imagestream.CopyTo(newStream, stat.cbSize, inBytes, outBytes); newStream.Commit(0); } catch (Exception r) { Debug.WriteLine(r.ToString()); throw; } finally { if (newStream != null) { Marshal.FinalReleaseComObject(newStream); } newStream = null; Marshal.FreeHGlobal(inBytes); Marshal.FreeHGlobal(outBytes); #if DEBUG tm.Stop(); Debug.WriteLine(string.Format("Time spent in CreateISOFile: {0} ms", tm.Elapsed.TotalMilliseconds.ToString("#,#"))); #endif } }
private void CreateProgressISOFile(COMTypes.IStream imagestream) { COMTypes.STATSTG stat; var bloksize = _fsres.BlockSize; long totalblocks = _fsres.TotalBlocks; #if DEBUG var tm = new Stopwatch(); tm.Start(); #endif imagestream.Stat(out stat, 0x01); if (stat.cbSize == totalblocks * bloksize) { var buff = new byte[bloksize]; var bw = new BinaryWriter(new FileStream(OutputFileName, FileMode.Create)); var pcbRead = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint))); var prg = _fsres.ProgressItems; var enm = prg.GetEnumerator(); enm.MoveNext(); var crtitem = enm.Current as IProgressItem; try { Marshal.WriteInt32(pcbRead, 0); for (float i = 0; i < totalblocks; i++) { imagestream.Read(buff, bloksize, pcbRead); if (Marshal.ReadInt32(pcbRead) != bloksize) { var err = string.Format( "Failed because Marshal.ReadInt32(pcbRead) = {0} != bloksize = {1}", Marshal.ReadInt32(pcbRead), bloksize); Debug.WriteLine(err); throw new ApplicationException(err); } bw.Write(buff); if (crtitem.LastBlock <= i) { if (enm.MoveNext()) { crtitem = enm.Current as IProgressItem; } if (Cancel) { return; } } if (Update != null) { Update(crtitem, i / totalblocks); } if (Cancel) { return; } } if (Update != null) { Update(crtitem, 1); } } catch (Exception ex) { Debug.WriteLine(string.Format("Exception in : CreateProgressISOFile {0}", ex.Message)); throw; } finally { bw.Flush(); bw.Close(); Marshal.FreeHGlobal(pcbRead); #if DEBUG tm.Stop(); Debug.WriteLine(string.Format("Time spent in CreateProgressISOFile: {0} ms", tm.Elapsed.TotalMilliseconds.ToString("#,#"))); #endif } } Debug.WriteLine("failed because stat.cbSize({0}) != totalblocks({1}) * bloksize({2}) ", stat.cbSize, totalblocks, bloksize); }