Windows实际上带有用于创建ISO的本机API以及更多:Image Mastering API.一旦从Windows SDK中获取IMAPI2 Interop程序集,或者(可能更容易)从此CodeProject文章中获取有关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
}
Run Code Online (Sandbox Code Playgroud)
可以在pinvoke.net上找到有关Win32/COM API粘合剂的详细信息:SHCreateStreamOnFile.IStream和STATSTG位于System.Runtime.InteropServices.ComTypes中..
| 归档时间: |
|
| 查看次数: |
6543 次 |
| 最近记录: |