共用方式為


建立多磁碟區光碟

映像製作 API (IMAPI)支援在下列多重區段光碟類型中新增和移除檔案:

  • CD-R/CD-RW
  • Single-Layer DVD+R/DVD-R
  • DVD+R 雙層
  • BD-R
  • DVD-RW/DVD+RW (僅限於 Windows 7
  • DVD-RAM (僅限 Windows 7
  • BD-RE (僅限 Windows 7

使用 IMAPI 建立多磁碟區光碟包含下列步驟。 每一個記載的步驟都包含完整 Visual Basic 腳本範例在最後一節中提供的相關部分。

初始化光碟錄製器

在裝置初始化之前,MsftDiscMaster2 物件會提供系統上光學裝置的列舉。 IDiscMaster2 介面可讓您存取此裝置列舉,以便找到合適的錄製裝置。 MsftDiscMaster2 物件也會在電腦新增或移除光學設備時提供事件通知。

找到光學錄製器並擷取指派給它的標識符之後,請建立新的 MsftDiscMaster2 物件,並使用特定裝置識別元初始化錄製器。

IDiscRecorder2 介面可讓您存取基本的裝置資訊,例如廠商標識碼、產品識別碼、產品修訂,以及退出媒體或關閉匣的方法。

注意

下列範例中宣告的其他常數和維度稍後會用於本文件最後一節的完整範例腳本中。 初始化光碟錄製器的行為不需要這些元素。

 

' *** 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::put_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::put_MultisessionInterfaces 屬性應該先初始化,再呼叫 IFileSystemImage::ImportFileSystemIFileSystemImage::ImportSpecificFileSystem 方法,從最後記錄的會話匯入文件系統。 這些方法會自動填入 MsftFileSystemImage 物件,其中包含描述先前記錄的檔案和目錄的資訊。

注意

IFileSystemImage::put_MultisessionInterfaces 屬性通常會使用資料寫入器透過 IDiscFormat2Data::get_MultisessionInterfaces 屬性所提供的多工作業介面來進行初始化。

 

如果 IMAPI 不支援目前插入媒體的多重燒錄或因其他原因(例如媒體已封閉)無法附加,則嘗試設定 IFileSystemImage::put_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::RemoveIFsiDirectoryItem::RemoveTree透過 IFsiDirectoryItem 介面表示之目錄物件的方法。 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