Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Open Method (String, FileMode)

Package.Open Method (String, FileMode)

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

Namespace:  System.IO.Packaging
Assembly:  WindowsBase (in WindowsBase.dll)

public static Package Open(
	string path,
	FileMode packageMode


Type: System.String

The path and file name of the package.

Type: System.IO.FileMode

The file mode in which to open the package.

Return Value

Type: System.IO.Packaging.Package
The opened package.


path is null.


Value for packageMode is not valid.

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 http://go.microsoft.com/fwlink/?LinkID=71255.

The following example shows how to create a new Package that incorporates PackageRelationship and PackagePart elements together with stored data. For the complete sample, see Writing a Package Sample.

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

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

        // Add a Resource Part to the Package
        PackagePart packagePartResource =

        // 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
                                new Uri(@"../resources/image1.jpg",

    }// 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()

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft