Package.GetPart(Uri) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回具有给定 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
的部件。
例外
partUri
为 null
。
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 文件返回请求的部件。
有关其他信息,请参阅开放打包约定 (OPC) 规范,可从 下载 https://www.ecma-international.org/publications-and-standards/standards/ecma-376/。
继承者说明
GetPart(Uri) 在内部调用派生类方法,根据派生类 GetPartCore(Uri) 中实现的物理格式实际刷新部件。