我复制了一个使用 IMAPI 编写 ISO 文件的程序。它似乎截断了目录名称。例如,如果我调用这个函数:
image.Root.AddTree("C:\\DIR\\SUBDIR", true);
我在 ISO 中得到的是:
SUBDIR\file 1
SUBDIR\file 2
...
“DIR”部分被删除。
我想要的是:
DIR\SUBDIR\file 1
DIR\SUBDIR\file 2
...
我已经看到Create a folder in disk and Burn Into it using IMAPI 2,其中一个回复建议使用
rootItem.AddDirectory(directoryName);
rootItem.get_Item(directoryName, newDirItem);
但get_Item
会导致编译错误“无法显式调用运算符或访问器”
有关如何在 ISO 文件中显式创建目录(并写入目录)的任何建议?
这是代码。将 IMAPI2 和 IMAPI2FS 添加到引用中。
using System;
using System.IO;
using System.Runtime.InteropServices;
using IMAPI2FS; // COM reference: Microsoft IMAPI2 File System Image Creator
using IStream = System.Runtime.InteropServices.ComTypes.IStream;
using STATSTG = System.Runtime.InteropServices.ComTypes.STATSTG;
namespace CreateISO
{
class IsoImage
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, PreserveSig = true)]
static extern void SHCreateStreamOnFile(string pszFile, uint grfMode, out IStream ppstm);
public void CreateISO(string path)
{
var image = (IFileSystemImage)new MsftFileSystemImage(); // same as Activator.CreateInstance(Type.GetTypeFromProgID("IMAPI2FS.MsftFileSystemImage"))
image.ChooseImageDefaultsForMediaType(IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DISK);
image.FileSystemsToCreate = FsiFileSystems.FsiFileSystemJoliet | FsiFileSystems.FsiFileSystemISO9660;
image.VolumeName = Path.GetDirectoryName(path);
image.Root.AddTree(path, true);
var inStream = (IStream)image.CreateResultImage().ImageStream;
STATSTG stat;
inStream.Stat(out stat, 0x01);
IStream outStream;
SHCreateStreamOnFile("C:\\temp\\demo.iso", 0x00001001, out outStream);
unsafe
{
long inBytes;
long outBytes;
long* pin = &inBytes;
long* pout = &outBytes;
inStream.CopyTo(outStream, stat.cbSize, (IntPtr)pin, (IntPtr)pout);
outStream.Commit(0);
}
}
}
}