Share via


Package.GetPart(Uri) Metoda

Definice

Vrátí část s daným identifikátorem 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

Parametry

partUri
Uri

Identifikátor URI (Uniform Resource Identifier) části, která se má vrátit.

Návraty

Část se zadaným partUriparametrem .

Výjimky

partUri je null.

partUri není platný PackagePart identifikátor URI (Uniform Resource Identifier).

Část se zadaným partUri parametrem není v balíčku.

Balíček není otevřený (Dispose(Boolean) nebo Close() byl volána).

Balíček je jen pro zápis.

Příklady

Následující příklad ukazuje, jak najít, načíst a číst části obsažené v balíčku.

// 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.

Poznámky

Vyvolá InvalidOperationException se, pokud část se zadaným partUri objektem neexistuje.

Metodu PartExists lze použít k určení, zda partUri odkazuje na existující část.

Ve výchozím nastavení ZipPackage je k dispozici a používána odvozená implementace abstraktní Package základní třídy. Ve výchozí operaci interně volá GetPartCoreZipPackage třídu, GetPart aby vrátila požadovanou část ze souboru ZIP.

Další informace najdete ve specifikaci OPC (Open Packaging Conventions), která je k dispozici ke stažení na adrese https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Poznámky pro dědice

GetPart(Uri) interně volá metodu odvozené třídy GetPartCore(Uri) , aby skutečně vyprázdnil část na základě fyzického formátu implementovaného v odvozené třídě.

Platí pro

Viz také