Share via


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(Uniform Resource Identifier)입니다.

반환

지정된 partUri의 파트입니다.

예외

partUri이(가) null인 경우

partUri는 유효한 PackagePart URI(Uniform Resource Identifier)가 아닙니다.

지정된 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 가 있는 파트가 없으면 이 throw됩니다.

메서드를 PartExists 사용하여 기존 부분을 참조하는지 여부를 partUri 확인할 수 있습니다.

기본적으로 ZipPackage 추상 Package 기본 클래스의 파생된 구현이 제공되고 사용됩니다. 기본 작업 GetPart 에서 내부적으로 클래스를 ZipPackage 호출 GetPartCore 하여 ZIP 파일에서 요청된 부분을 반환합니다.

자세한 내용은 에서 https://www.ecma-international.org/publications-and-standards/standards/ecma-376/다운로드할 수 있는 OPC(Open Packaging Conventions) 사양을 참조하세요.

상속자 참고

GetPart(Uri) 는 파생 클래스 메서드를 내부적으로 호출하여 파생 클래스 GetPartCore(Uri) 에 구현된 실제 형식에 따라 파트를 실제로 플러시합니다.

적용 대상

추가 정보