创建多盘光盘

映像主控 API (IMAPI) 支持在以下多盘光盘类型中添加和删除文件:

  • CD-R/CD-RW
  • Single-Layer DVD+R/DVD-R
  • DVD+R 双层
  • BD-R
  • DVD-RW/DVD+RW (仅限 Windows 7)
  • 仅 windows 7 (DVD-RAM)
  • BD-RE 仅 (Windows 7)

使用 IMAPI 创建多盘光盘包括以下步骤。 其中每个记录的步骤都包含最后一节中提供的完整 Visual Basic 脚本示例的相关部分。

初始化光盘录制器

在设备初始化之前, MsftDiscMaster2 对象提供系统上光学设备的枚举。 IDiscMaster2 接口提供对此设备枚举的访问,以便于适当录制设备的位置。 在计算机中添加或删除光学设备时, MsftDiscMaster2 对象还提供事件通知。

找到光学记录器并检索分配给它的 ID 后,创建一个新的 MsftDiscMaster2 对象并使用特定设备 ID 初始化该记录器。

IDiscRecorder2 接口提供对基本设备信息(如供应商 ID、产品 ID、产品修订版)的访问,以及弹出介质或关闭托盘的方法。

注意

以下示例中声明的其他常量和维度稍后将在本文档最后一节的完整示例脚本中使用。 初始化光盘录音机的操作不需要这些元素。

 

' *** CD/DVD disc file system types
Const FsiFileSystemISO9660 = 1
Const FsiFileSystemJoliet  = 2
Const FsiFileSystemUDF102  = 4

WScript.Quit(Main)

Function Main
    Dim Index                ' Index to recording drive.
    Dim Recorder             ' Recorder object
    Dim Path                 ' Directory of files to add
    Dim Stream               ' Data stream for burning device
    
    Index = 0                ' First drive on the system
    Path = "G:\BurnDir"      ' Files to add to the disc

    ' Create a DiscMaster2 object to connect to optical drives.
    Dim DiscMaster
    Set DiscMaster = WScript.CreateObject("IMAPI2.MsftDiscMaster2")

    ' Create a DiscRecorder2 object for the specified burning device.
    Dim UniqueId
    set Recorder = WScript.CreateObject("IMAPI2.MsftDiscRecorder2")
    UniqueId = DiscMaster.Item(Index)
    Recorder.InitializeDiscRecorder(UniqueId)

创建数据编写器

MsftDiscFormat2Data 对象提供写入方法、其属性以及特定于媒体的属性。 IDiscFormat2Data 接口提供对此对象的访问。

光盘录制器使用 IDiscFormat2Data::p ut_Recorder 属性绑定到格式编写器。 将记录器绑定到格式编写器后,可以在使用 IDiscFormat2Data::Write 方法将结果图像写入光盘之前执行媒体和特定于写入的属性查询。

注意

下面的示例代码中指定的客户端名称字符串应根据特定应用程序进行调整。

 

    ' Create a DiscFormat2Data object and set the recorder
    Dim DataWriter
    Set DataWriter = CreateObject ("IMAPI2.MsftDiscFormat2Data")
    DataWriter.Recorder = Recorder
    DataWriter.ClientName = "IMAPIv2 TEST"

创建文件系统对象

若要录制新会话,必须先为其生成刻录图像。 ISO9660JolietUDF 格式的刻录映像由单个文件和目录的文件系统组成。 MsftFileSystemImage 对象是包含要放置在光学介质上的文件和目录的文件系统对象。 IFileSystemImage 接口提供对文件系统对象和设置的访问权限。

    ' Create a new file system image object
    Dim FSI
    Set FSI = CreateObject("IMAPI2FS.MsftFileSystemImage")

导入文件系统

在继续之前,通过检查 IDiscFormat2::get_MediaHeuristicallyBlank 属性确保光盘不为空。

创建 MsftFileSystemImage 对象后,应先初始化 IFileSystemImage::p ut_MultisessionInterfaces 属性,然后再调用 IFileSystemImage::ImportFileSystemImage::ImportFileSystemImage::ImportSpecificFileSystem 方法,以便从上次记录的会话导入文件系统。 这些方法将自动使用描述以前记录的文件和目录的信息填充 MsftFileSystemImage 对象。

注意

IFileSystemImage::p ut_MultisessionInterfaces 属性通常使用数据编写器通过 IDiscFormat2Data::get_MultisessionInterfaces 属性提供的 multisession 接口进行初始化。

 

如果 IMAPI 不支持对当前插入的媒体进行多重设置,或者由于某种其他原因而无法追加媒体, (尝试设置 IFileSystemImage::p ut_MultisessionInterfaces 属性将失败,因为该媒体) 关闭。

如果上一个刻录会话包含多个文件系统类型, 则 IFileSystemImage::ImportFileSystem 方法将从现有最高级文件系统类型导入信息。 例如,在本主题提供的示例中,UDF 是导入的文件系统。 但是,使用 IFileSystemImage::ImportSpecificFileSystem 方法可以导入文件系统的特定选择。

注意

IFileSystemImage::IdentifyFileSystemsOnDisc 方法可用于确定光盘上可用的文件系统。

 

    ' Import the last session, if the disc is not empty, or initialize
    ' the file system, if the disc is empty
    If Not DataWriter.MediaHeuristicallyBlank _
    Then
        On Error Resume Next
        FSI.MultisessionInterfaces = DataWriter.MultisessionInterfaces
        If Err.Number <> 0 _
        Then
            WScript.Echo "Multisession is not supported for this disc"
            Main = 1
            Exit Function
        End If
        On Error Goto 0

        WScript.Echo "Importing data from the previous session..."
        FSI.ImportFileSystem()
    Else 
        FSI.ChooseImageDefaults(Recorder)
    End If

在文件系统中添加或删除文件

创建文件系统对象并从上一个会话导入文件系统后,请分别调用 IFileSystemImage::CreateFileItemIFileSystemImage::CreateDirectoryItem 方法来创建新的文件和目录对象。 文件和目录对象提供有关文件和目录的特定详细信息。 或者,目录对象的 IFsiDirectoryItem::AddTree 方法(通过 IFsiDirectoryItem 接口表示)可用于从另一个存储设备(即硬盘驱动器 () )添加现有文件和目录。

可用于 IFileSystemImage 的事件处理程序更新方法标识要添加到文件系统映像的当前文件、已复制的扇区数以及要复制的扇区总数。

若要从文件系统中删除现有文件和目录,请使用通过 IFsiDirectoryItem 接口表示的目录对象的 IFsiDirectoryItem::Remove 和 IFsiDirectoryItem::RemoveTree 方法。 IFileSystemImage::get_Root 属性用于获取指向文件系统根目录的指针,以及要遍历目录树的 IFsiDirectoryItem 接口。

注意

通过 IFsiDirectoryItem::RemoveIFsiDirectoryItem::RemoveTree 方法删除的文件和目录不会从光盘中实际删除,高级软件可以轻松恢复已删除的信息。

 

    ' Add the directory and its contents to the file system 
    WScript.Echo "Adding " & Path & " directory to the disc..."
    FSI.Root.AddTree Path, false

构造文件系统映像

最后一步是调用 IFileSystemImage::CreateResultImage 为刻录映像创建数据流,并通过 IFileSystemImageResult 接口提供对它的访问。 此数据流可以直接提供给 IDiscFormat2Data::Write 方法,也可以保存到文件供以后使用。

    ' Create an image from the file system image object
    Dim Result
    Set Result = FSI.CreateResultImage()
    Stream = Result.ImageStream

示例摘要

以下 Visual Basic 脚本示例演示如何使用 IMAPI 对象创建多组光盘。 该示例创建一个新会话,并将目录添加到光盘。为简单起见,该代码不执行广泛的错误检查,并假定以下内容:

  • 系统上安装了兼容的光盘设备。
  • 光盘设备是系统上的第一个驱动器。
  • 在光盘设备中插入兼容的光盘。
  • 要添加到光盘的文件位于“g:\burndir”中。

可以将其他功能(如广泛的错误检查、设备和媒体兼容性、事件通知以及光盘上的可用空间计算)添加到脚本中。

' This script adds data files from a single directory tree to a
' disc (a new session is added, if the disc already contains data)

' Copyright (C) Microsoft. All rights reserved.

Option Explicit

' *** CD/DVD disc file system types
Const FsiFileSystemISO9660 = 1
Const FsiFileSystemJoliet  = 2
Const FsiFileSystemUDF102  = 4

WScript.Quit(Main)

Function Main
    Dim Index                ' Index to recording drive.
    Dim Recorder             ' Recorder object
    Dim Path                 ' Directory of files to add
    Dim Stream               ' Data stream for burning device
    
    Index = 0                ' First drive on the system
    Path = "G:\BurnDir"      ' Files to add to the disc

    ' Create a DiscMaster2 object to connect to optical drives.
    Dim DiscMaster
    Set DiscMaster = WScript.CreateObject("IMAPI2.MsftDiscMaster2")

    ' Create a DiscRecorder2 object for the specified burning device.
    Dim UniqueId
    set Recorder = WScript.CreateObject("IMAPI2.MsftDiscRecorder2")
    UniqueId = DiscMaster.Item(Index)
    Recorder.InitializeDiscRecorder(UniqueId)

    ' Create a DiscFormat2Data object and set the recorder
    Dim DataWriter
    Set DataWriter = CreateObject ("IMAPI2.MsftDiscFormat2Data")
    DataWriter.Recorder = Recorder
    DataWriter.ClientName = "IMAPIv2 TEST"

    ' Create a new file system image object
    Dim FSI
    Set FSI = CreateObject("IMAPI2FS.MsftFileSystemImage")

    ' Import the last session, if the disc is not empty, or initialize
    ' the file system, if the disc is empty
    If Not DataWriter.MediaHeuristicallyBlank _
    Then
        On Error Resume Next
        FSI.MultisessionInterfaces = DataWriter.MultisessionInterfaces
        If Err.Number <> 0 _
        Then
            WScript.Echo "Multisession is not supported for this disc"
            Main = 1
            Exit Function
        End If
        On Error Goto 0

        WScript.Echo "Importing data from the previous session..."
        FSI.ImportFileSystem()
    Else 
        FSI.ChooseImageDefaults(Recorder)
    End If

    ' Add the directory and its contents to the file system 
    WScript.Echo "Adding " & Path & " directory to the disc..."
    FSI.Root.AddTree Path, false

    ' Create an image from the file system image object
    Dim Result
    Set Result = FSI.CreateResultImage()
    Stream = Result.ImageStream
    
    ' Write stream to disc using the specified recorder
    WScript.Echo "Writing content to the disc..."
    DataWriter.Write(Stream)

    WScript.Echo "Finished writing content."
    Main = 0
End Function

使用 IMAPI

IStream

IDiscMaster2

IDiscFormat2Data

IFileSystemImage