Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


Package.Open Method

Definition

Opens a package.

Overloads

Open(Stream)

Opens a package on a given IO stream.

Open(String)

Opens a package at a given path and file name.

Open(Stream, FileMode)

Opens a package with a given IO stream and file mode.

Open(String, FileMode)

Opens a package at a given path using a given file mode.

Open(Stream, FileMode, FileAccess)

Opens a package with a given IO stream, file mode, and file access setting.

Open(String, FileMode, FileAccess)

Opens a package at a given path using a given file mode and file access setting.

Open(String, FileMode, FileAccess, FileShare)

Opens a package at a given path using a given file mode, file access, and file share setting.

Examples

The following example shows how to create a new Package that incorporates PackageRelationship and PackagePart elements together with stored data.

C#
//  -------------------------- CreatePackage --------------------------
/// <summary>
///   Creates a package zip file containing specified
///   content and resource files.</summary>
private static void CreatePackage()
{
    // Convert system path and file names to Part URIs. In this example
    // Uri partUriDocument /* /Content/Document.xml */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Content\Document.xml", UriKind.Relative));
    // Uri partUriResource /* /Resources/Image1.jpg */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Resources\Image1.jpg", UriKind.Relative));
    Uri partUriDocument = PackUriHelper.CreatePartUri(
                              new Uri(documentPath, UriKind.Relative));
    Uri partUriResource = PackUriHelper.CreatePartUri(
                              new Uri(resourcePath, UriKind.Relative));

    // Create the Package
    // (If the package file already exists, FileMode.Create will
    //  automatically delete it first before creating a new one.
    //  The 'using' statement insures that 'package' is
    //  closed and disposed when it goes out of scope.)
    using (Package package =
        Package.Open(packagePath, FileMode.Create))
    {
        // Add the Document part to the Package
        PackagePart packagePartDocument =
            package.CreatePart(partUriDocument,
                           System.Net.Mime.MediaTypeNames.Text.Xml);

        // Copy the data to the Document Part
        using (FileStream fileStream = new FileStream(
               documentPath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartDocument.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add a Package Relationship to the Document Part
        package.CreateRelationship(packagePartDocument.Uri,
                                   TargetMode.Internal,
                                   PackageRelationshipType);

        // Add a Resource Part to the Package
        PackagePart packagePartResource =
            package.CreatePart(partUriResource,
                           System.Net.Mime.MediaTypeNames.Image.Jpeg);

        // Copy the data to the Resource Part
        using (FileStream fileStream = new FileStream(
               resourcePath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartResource.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add Relationship from the Document part to the Resource part
        packagePartDocument.CreateRelationship(
                                new Uri(@"../resources/image1.jpg",
                                UriKind.Relative),
                                TargetMode.Internal,
                                ResourceRelationshipType);
    }// end:using (Package package) - Close and dispose package.
}// end:CreatePackage()

//  --------------------------- CopyStream ---------------------------
/// <summary>
///   Copies data from a source stream to a target stream.</summary>
/// <param name="source">
///   The source stream to copy from.</param>
/// <param name="target">
///   The destination stream to copy to.</param>
private static void CopyStream(Stream source, Stream target)
{
    const int bufSize = 0x1000;
    byte[] buf = new byte[bufSize];
    int bytesRead = 0;
    while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
        target.Write(buf, 0, bytesRead);
}// end:CopyStream()

Remarks

ZipPackage is the default package type that is used by the Open method.

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Open(Stream)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

Opens a package on a given IO stream.

C#
public static System.IO.Packaging.Package Open(System.IO.Stream stream);

Parameters

stream
Stream

The IO stream on which to open the package.

Returns

The opened package.

Exceptions

stream is null.

The package to open requires read or read/write permission and the specified stream is write-only; or, the package to open requires write or read/write permission and the specified stream is read-only.

Remarks

ZipPackage is the default package type that is used by the Open method.

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Applies to

.NET 10 (package-provided) és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Open(String)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

Opens a package at a given path and file name.

C#
public static System.IO.Packaging.Package Open(string path);

Parameters

path
String

The path and file name of the package.

Returns

The opened package.

Exceptions

path is null.

Examples

The following example shows how to create a new Package that incorporates PackageRelationship and PackagePart elements together with stored data.

C#
//  -------------------------- CreatePackage --------------------------
/// <summary>
///   Creates a package zip file containing specified
///   content and resource files.</summary>
private static void CreatePackage()
{
    // Convert system path and file names to Part URIs. In this example
    // Uri partUriDocument /* /Content/Document.xml */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Content\Document.xml", UriKind.Relative));
    // Uri partUriResource /* /Resources/Image1.jpg */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Resources\Image1.jpg", UriKind.Relative));
    Uri partUriDocument = PackUriHelper.CreatePartUri(
                              new Uri(documentPath, UriKind.Relative));
    Uri partUriResource = PackUriHelper.CreatePartUri(
                              new Uri(resourcePath, UriKind.Relative));

    // Create the Package
    // (If the package file already exists, FileMode.Create will
    //  automatically delete it first before creating a new one.
    //  The 'using' statement insures that 'package' is
    //  closed and disposed when it goes out of scope.)
    using (Package package =
        Package.Open(packagePath, FileMode.Create))
    {
        // Add the Document part to the Package
        PackagePart packagePartDocument =
            package.CreatePart(partUriDocument,
                           System.Net.Mime.MediaTypeNames.Text.Xml);

        // Copy the data to the Document Part
        using (FileStream fileStream = new FileStream(
               documentPath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartDocument.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add a Package Relationship to the Document Part
        package.CreateRelationship(packagePartDocument.Uri,
                                   TargetMode.Internal,
                                   PackageRelationshipType);

        // Add a Resource Part to the Package
        PackagePart packagePartResource =
            package.CreatePart(partUriResource,
                           System.Net.Mime.MediaTypeNames.Image.Jpeg);

        // Copy the data to the Resource Part
        using (FileStream fileStream = new FileStream(
               resourcePath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartResource.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add Relationship from the Document part to the Resource part
        packagePartDocument.CreateRelationship(
                                new Uri(@"../resources/image1.jpg",
                                UriKind.Relative),
                                TargetMode.Internal,
                                ResourceRelationshipType);
    }// end:using (Package package) - Close and dispose package.
}// end:CreatePackage()

//  --------------------------- CopyStream ---------------------------
/// <summary>
///   Copies data from a source stream to a target stream.</summary>
/// <param name="source">
///   The source stream to copy from.</param>
/// <param name="target">
///   The destination stream to copy to.</param>
private static void CopyStream(Stream source, Stream target)
{
    const int bufSize = 0x1000;
    byte[] buf = new byte[bufSize];
    int bytesRead = 0;
    while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
        target.Write(buf, 0, bytesRead);
}// end:CopyStream()

Remarks

ZipPackage is the default package type that is used by the Open method.

This Open method opens the package with default attributes OpenOrCreate, ReadWrite, and None (to specify different attributes use one of the other Open method overloads).

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Applies to

.NET 10 (package-provided) és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Open(Stream, FileMode)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

Opens a package with a given IO stream and file mode.

C#
public static System.IO.Packaging.Package Open(System.IO.Stream stream, System.IO.FileMode packageMode);

Parameters

stream
Stream

The IO stream on which to open the package.

packageMode
FileMode

The file mode in which to open the package.

Returns

The opened package.

Exceptions

stream is null.

packageMode value is not valid.

The package to open requires read or read/write permission and the specified stream is write-only; or, the package to open requires write or read/write permission and the specified stream is read-only.

Remarks

ZipPackage is the default package type that is used by the Open method.

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Applies to

.NET 10 (package-provided) és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Open(String, FileMode)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

Opens a package at a given path using a given file mode.

C#
public static System.IO.Packaging.Package Open(string path, System.IO.FileMode packageMode);

Parameters

path
String

The path and file name of the package.

packageMode
FileMode

The file mode in which to open the package.

Returns

The opened package.

Exceptions

path is null.

Value for packageMode is not valid.

Examples

The following example shows how to create a new Package that incorporates PackageRelationship and PackagePart elements together with stored data.

C#
//  -------------------------- CreatePackage --------------------------
/// <summary>
///   Creates a package zip file containing specified
///   content and resource files.</summary>
private static void CreatePackage()
{
    // Convert system path and file names to Part URIs. In this example
    // Uri partUriDocument /* /Content/Document.xml */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Content\Document.xml", UriKind.Relative));
    // Uri partUriResource /* /Resources/Image1.jpg */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Resources\Image1.jpg", UriKind.Relative));
    Uri partUriDocument = PackUriHelper.CreatePartUri(
                              new Uri(documentPath, UriKind.Relative));
    Uri partUriResource = PackUriHelper.CreatePartUri(
                              new Uri(resourcePath, UriKind.Relative));

    // Create the Package
    // (If the package file already exists, FileMode.Create will
    //  automatically delete it first before creating a new one.
    //  The 'using' statement insures that 'package' is
    //  closed and disposed when it goes out of scope.)
    using (Package package =
        Package.Open(packagePath, FileMode.Create))
    {
        // Add the Document part to the Package
        PackagePart packagePartDocument =
            package.CreatePart(partUriDocument,
                           System.Net.Mime.MediaTypeNames.Text.Xml);

        // Copy the data to the Document Part
        using (FileStream fileStream = new FileStream(
               documentPath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartDocument.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add a Package Relationship to the Document Part
        package.CreateRelationship(packagePartDocument.Uri,
                                   TargetMode.Internal,
                                   PackageRelationshipType);

        // Add a Resource Part to the Package
        PackagePart packagePartResource =
            package.CreatePart(partUriResource,
                           System.Net.Mime.MediaTypeNames.Image.Jpeg);

        // Copy the data to the Resource Part
        using (FileStream fileStream = new FileStream(
               resourcePath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartResource.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add Relationship from the Document part to the Resource part
        packagePartDocument.CreateRelationship(
                                new Uri(@"../resources/image1.jpg",
                                UriKind.Relative),
                                TargetMode.Internal,
                                ResourceRelationshipType);
    }// end:using (Package package) - Close and dispose package.
}// end:CreatePackage()

//  --------------------------- CopyStream ---------------------------
/// <summary>
///   Copies data from a source stream to a target stream.</summary>
/// <param name="source">
///   The source stream to copy from.</param>
/// <param name="target">
///   The destination stream to copy to.</param>
private static void CopyStream(Stream source, Stream target)
{
    const int bufSize = 0x1000;
    byte[] buf = new byte[bufSize];
    int bytesRead = 0;
    while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
        target.Write(buf, 0, bytesRead);
}// end:CopyStream()

Remarks

ZipPackage is the default package type that is used by the Open method.

This Open method opens the package with default attributes ReadWrite and None (to specify different attributes use one of the other Open method overloads).

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Applies to

.NET 10 (package-provided) és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Open(Stream, FileMode, FileAccess)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

Opens a package with a given IO stream, file mode, and file access setting.

C#
public static System.IO.Packaging.Package Open(System.IO.Stream stream, System.IO.FileMode packageMode, System.IO.FileAccess packageAccess);

Parameters

stream
Stream

The IO stream on which to open the package.

packageMode
FileMode

The file mode in which to open the package.

packageAccess
FileAccess

The file access in which to open the package.

Returns

The opened package.

Exceptions

stream is null.

The value for packageMode or packageAccess is not valid.

The package to open requires read or read/write permission and the specified stream is write-only; or the package to open requires write or read/write permission and the specified stream is read-only.

Remarks

ZipPackage is the default package type that is used by the Open method.

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Applies to

.NET 10 (package-provided) és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Open(String, FileMode, FileAccess)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

Opens a package at a given path using a given file mode and file access setting.

C#
public static System.IO.Packaging.Package Open(string path, System.IO.FileMode packageMode, System.IO.FileAccess packageAccess);

Parameters

path
String

The path and file name of the package.

packageMode
FileMode

The file mode in which to open the package.

packageAccess
FileAccess

The file access in which to open the package.

Returns

The opened package.

Exceptions

path is null.

Value for packageMode or packageAccess is not valid.

Examples

The following example shows how to open and read a Package that contains PackageRelationship and PackagePart elements together with stored data.

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

Remarks

ZipPackage is the default package type that is used by the Open method.

This Open method opens the package with a default None attribute (to specify a different attribute use the Open method overload).

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Applies to

.NET 10 (package-provided) és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Open(String, FileMode, FileAccess, FileShare)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

Opens a package at a given path using a given file mode, file access, and file share setting.

C#
public static System.IO.Packaging.Package Open(string path, System.IO.FileMode packageMode, System.IO.FileAccess packageAccess, System.IO.FileShare packageShare);

Parameters

path
String

The path and file name of the package.

packageMode
FileMode

The file mode in which to open the package.

packageAccess
FileAccess

The file access in which to open the package.

packageShare
FileShare

The file sharing mode in which to open the package.

Returns

The opened package.

Exceptions

path is null.

The value for packageMode, packageAccess, or packageShare is not valid.

Examples

The following example shows how to create a new Package that incorporates PackageRelationship and PackagePart elements together with stored data.

C#
//  -------------------------- CreatePackage --------------------------
/// <summary>
///   Creates a package zip file containing specified
///   content and resource files.</summary>
private static void CreatePackage()
{
    // Convert system path and file names to Part URIs. In this example
    // Uri partUriDocument /* /Content/Document.xml */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Content\Document.xml", UriKind.Relative));
    // Uri partUriResource /* /Resources/Image1.jpg */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Resources\Image1.jpg", UriKind.Relative));
    Uri partUriDocument = PackUriHelper.CreatePartUri(
                              new Uri(documentPath, UriKind.Relative));
    Uri partUriResource = PackUriHelper.CreatePartUri(
                              new Uri(resourcePath, UriKind.Relative));

    // Create the Package
    // (If the package file already exists, FileMode.Create will
    //  automatically delete it first before creating a new one.
    //  The 'using' statement insures that 'package' is
    //  closed and disposed when it goes out of scope.)
    using (Package package =
        Package.Open(packagePath, FileMode.Create))
    {
        // Add the Document part to the Package
        PackagePart packagePartDocument =
            package.CreatePart(partUriDocument,
                           System.Net.Mime.MediaTypeNames.Text.Xml);

        // Copy the data to the Document Part
        using (FileStream fileStream = new FileStream(
               documentPath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartDocument.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add a Package Relationship to the Document Part
        package.CreateRelationship(packagePartDocument.Uri,
                                   TargetMode.Internal,
                                   PackageRelationshipType);

        // Add a Resource Part to the Package
        PackagePart packagePartResource =
            package.CreatePart(partUriResource,
                           System.Net.Mime.MediaTypeNames.Image.Jpeg);

        // Copy the data to the Resource Part
        using (FileStream fileStream = new FileStream(
               resourcePath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartResource.GetStream());
        }// end:using(fileStream) - Close and dispose fileStream.

        // Add Relationship from the Document part to the Resource part
        packagePartDocument.CreateRelationship(
                                new Uri(@"../resources/image1.jpg",
                                UriKind.Relative),
                                TargetMode.Internal,
                                ResourceRelationshipType);
    }// end:using (Package package) - Close and dispose package.
}// end:CreatePackage()

//  --------------------------- CopyStream ---------------------------
/// <summary>
///   Copies data from a source stream to a target stream.</summary>
/// <param name="source">
///   The source stream to copy from.</param>
/// <param name="target">
///   The destination stream to copy to.</param>
private static void CopyStream(Stream source, Stream target)
{
    const int bufSize = 0x1000;
    byte[] buf = new byte[bufSize];
    int bytesRead = 0;
    while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
        target.Write(buf, 0, bytesRead);
}// end:CopyStream()

Remarks

ZipPackage is the default package type that is used by the Open method.

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

Applies to

.NET 10 (package-provided) és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10