2012-03-13 101 views
4

什麼是基於來自C#中的文件夾結構.ISO文件的最佳方式?是否有除DiscUtils以外的開源庫?運行到問題與DiscUtils,我想嘗試另一種路徑。我有什麼選擇?我更喜歡開源,但也可能願意爲解決方案付費。DVD ISO從C# - .NET DiscUtils替代

+2

使用** ** DiscUtils對當前項目的要求......是你有什麼確切的問題考慮? – SliverNinja 2014-09-04 14:52:26

回答

5

Windows實際上附帶使用本地API創建ISO等等:Image Mastering API。一旦從Windows SDK獲取IMAPI2 Interop程序集或從this CodeProject article about IMAPI2(可能更容易),創建ISO只是幾行代碼。

不幸的是,你還需要幾段妥善清理所涉及的COM廢話,但最終的結果還是蠻管理:

MsftFileSystemImage iso = new MsftFileSystemImage(); 
iso.ChooseImageDefaultsForMediaType(IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DISK); 
iso.FileSystemsToCreate = FsiFileSystems.FsiFileSystemISO9660 | FsiFileSystems.FsiFileSystemJoliet; 

iso.Root.AddTree(@"c:\path\goes\here\", false); 

dynamic resultImage = iso.CreateResultImage(); 
dynamic imageStream = resultImage.ImageStream; 
IStream newStream = null; 

if (imageStream != null) 
{ 
    STATSTG stat = null; 
    imageStream.Stat(stat, 0x1); 

    int res = SHCreateStreamOnFile(@"c:\path\to\your.iso", 0x1001, newStream); 
    if (res == 0 && newStream != null) 
    { 
     IntPtr inBytes = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(long))); 
     IntPtr outBytes = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(long))); 
     try 
     { 
      imageStream.CopyTo(newStream, stat.cbSize, inBytes, outBytes); 
     } 
     finally 
     { 
      Marshal.FinalReleaseComObject(imageStream); 
      newStream.Commit(0); 
      Marshal.FinalReleaseComObject(newStream); 
      Marshal.FreeHGlobal(inBytes); 
      Marshal.FreeHGlobal(outBytes); 
      Marshal.FinalReleaseComObject(resultImage); 
      Marshal.FinalReleaseComObject(iso); 
     } 
    } 
    else 
    { 
     Marshal.FinalReleaseComObject(imageStream); 
     Marshal.FinalReleaseComObject(resultImage); 
     Marshal.FinalReleaseComObject(iso); 
     //TODO: Throw exception or do whatever to signal failure here 
    } 
}  
else 
{ 
    Marshal.FinalReleaseComObject(resultImage); 
    Marshal.FinalReleaseComObject(iso); 
    //TODO: Throw exception or do whatever to signal failure here 
} 

詳細爲Win32/COM API膠的使用可以唯一位SHCreateStreamOnFile:上pinvoke.net找到。 istream和STATSTG是System.Runtime.InteropServices.ComTypes.