建立多磁片區磁片

映射主控 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::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屬性,使用資料寫入器所提供的多重介面初始化。

 

如果 IMAPI 不支援目前插入媒體的多重介面,或媒體因其他原因而無法附加, (例如關閉) ,則嘗試設定 IFileSystemImage::p ut_MultisessionInterfaces 屬性將會失敗。

如果上一個的 burn 會話包含一個以上的檔案系統類型, 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介面表示之目錄物件的IFsiDirectoryItem::AddTree方法可用來從另一個儲存裝置新增現有的檔案和目錄 (,也就是硬碟) 。

IFileSystemImage可用的事件處理常式更新方法會識別要新增至檔案系統映射的目前檔案、已複製的磁區數目,以及要複製的磁區總數。

若要從檔案系統中移除現有的檔案和目錄,請使用透過 IFsiDirectoryItem介面表示之目錄物件的IFsiDirectoryItem::RemoveIFsiDirectoryItem::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