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 檔案傳回要求的元件。

如需詳細資訊,請參閱可在 下載 https://www.ecma-international.org/publications-and-standards/standards/ecma-376/的開放封裝慣例 (OPC) 規格。

給繼承者的注意事項

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

適用於

另請參閱