共用方式為


Package.GetPart(Uri) 方法

定義

傳回具有特定 URI 的組件。

public:
 System::IO::Packaging::PackagePart ^ GetPart(Uri ^ partUri);
public System.IO.Packaging.PackagePart GetPart (Uri partUri);
member this.GetPart : Uri -> System.IO.Packaging.PackagePart
Public Function GetPart (partUri As Uri) As PackagePart

參數

partUri
Uri

要傳回之組件的統一資源識別項 (URI)。

傳回

具有指定之 partUri 的組件。

例外狀況

partUrinull

partUri 不是有效的 PackagePart 統一資源識別項 (URI)。

具有指定之 partUri 的組件不在套件內。

套件沒有開啟 (已呼叫 Dispose(Boolean)Close())。

封裝是唯寫的。

範例

下列範例示範如何尋找、擷取和讀取包含在封裝中的元件。

// Open the Package.
// ('using' statement insures that 'package' is
//  closed and disposed when it goes out of scope.)
using (Package package =
    Package.Open(packagePath, FileMode.Open, FileAccess.Read))
{
    PackagePart documentPart = null;
    PackagePart resourcePart = null;

    // Get the Package Relationships and look for
    //   the Document part based on the RelationshipType
    Uri uriDocumentTarget = null;
    foreach (PackageRelationship relationship in
        package.GetRelationshipsByType(PackageRelationshipType))
    {
        // Resolve the Relationship Target Uri
        //   so the Document Part can be retrieved.
        uriDocumentTarget = PackUriHelper.ResolvePartUri(
            new Uri("/", UriKind.Relative), relationship.TargetUri);

        // Open the Document Part, write the contents to a file.
        documentPart = package.GetPart(uriDocumentTarget);
        ExtractPart(documentPart, targetDirectory);
    }

    // Get the Document part's Relationships,
    //   and look for required resources.
    Uri uriResourceTarget = null;
    foreach (PackageRelationship relationship in
        documentPart.GetRelationshipsByType(
                                ResourceRelationshipType))
    {
        // Resolve the Relationship Target Uri
        //   so the Resource Part can be retrieved.
        uriResourceTarget = PackUriHelper.ResolvePartUri(
            documentPart.Uri, relationship.TargetUri);

        // Open the Resource Part and write the contents to a file.
        resourcePart = package.GetPart(uriResourceTarget);
        ExtractPart(resourcePart, targetDirectory);
    }
}// end:using(Package package) - Close & dispose package.
' Open the Package.
' ('using' statement insures that 'package' is
'  closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Open, FileAccess.Read)
    Dim documentPart As PackagePart = Nothing
    Dim resourcePart As PackagePart = Nothing

    ' Get the Package Relationships and look for
    '   the Document part based on the RelationshipType
    Dim uriDocumentTarget As Uri = Nothing
    For Each relationship As PackageRelationship In package.GetRelationshipsByType(PackageRelationshipType)
        ' Resolve the Relationship Target Uri
        '   so the Document Part can be retrieved.
        uriDocumentTarget = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), relationship.TargetUri)

        ' Open the Document Part, write the contents to a file.
        documentPart = package.GetPart(uriDocumentTarget)
        ExtractPart(documentPart, targetDirectory)
    Next relationship

    ' Get the Document part's Relationships,
    '   and look for required resources.
    Dim uriResourceTarget As Uri = Nothing
    For Each relationship As PackageRelationship In documentPart.GetRelationshipsByType(ResourceRelationshipType)
        ' Resolve the Relationship Target Uri
        '   so the Resource Part can be retrieved.
        uriResourceTarget = PackUriHelper.ResolvePartUri(documentPart.Uri, relationship.TargetUri)

        ' Open the Resource Part and write the contents to a file.
        resourcePart = package.GetPart(uriResourceTarget)
        ExtractPart(resourcePart, targetDirectory)
    Next relationship

End Using ' end:using(Package package) - Close & dispose package.

備註

InvalidOperationException如果具有指定partUri之 的元件不存在,則會擲回 。

方法 PartExists 可用來判斷是否 partUri 參考現有的元件。

根據預設, ZipPackage 會提供及使用抽象 Package 基類的衍生實作。 在預設作業中, GetPart 會在內部呼叫 GetPartCoreZipPackage 類別,以從 ZIP 檔案傳回要求的元件。

如需詳細資訊,請參閱 open Packaging Conventions (OPC) specification for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/

給繼承者的注意事項

GetPart(Uri) 內部呼叫衍生類別方法,根據衍生類別 GetPartCore(Uri) 中實作的實體格式,實際排清元件。

適用於

另請參閱