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部分不存在,则会引发 anpartUri

该方法 PartExists 可用于确定是否 partUri 引用现有部件。

默认情况下, ZipPackage 提供和使用抽象 Package 基类的派生实现。 在默认操作中,GetPart从内部调用GetPartCoreZipPackage类以从 ZIP 文件返回请求的部件。

有关其他信息,请参阅可在以下位置 https://www.ecma-international.org/publications-and-standards/standards/ecma-376/下载的开放打包约定 (OPC) 规范。

继承者说明

GetPart(Uri) 内部调用派生类 GetPartCore(Uri) 方法,以基于派生类中实现的物理格式实际刷新部件。

适用于

另请参阅