Package.CreatePart Method (Uri, String, CompressionOption)

Creates a new part with a given URI, content type, and compression option.

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

public PackagePart CreatePart(
	Uri partUri,
	string contentType,
	CompressionOption compressionOption
)

Parameters

partUri
Type: System.Uri

The URI of the new part.

contentType
Type: System.String

The content type of the data stream.

compressionOption
Type: System.IO.Packaging.CompressionOption

The compression option for the data stream, NotCompressed or Normal compression.

Return Value

Type: System.IO.Packaging.PackagePart
The new created part.

ExceptionCondition
ArgumentNullException

partUri or contentType is null.

ArgumentException

partUri is not a valid PackagePart uniform resource identifier (URI).

InvalidOperationException

A part with the specified partUri is already present in the package.

ArgumentOutOfRangeException

The compressionOption value is not valid.

ObjectDisposedException

The package is not open (Dispose or Close has been called).

IOException

The package is read-only (a new part cannot be added).

For the default ZipPackage derived class, the CreatePart method only supports two compressionOption values, NotCompressed or Normal compression. Other CompressionOption values of Maximum, Fast, or SuperFast use Normal compression.

CreatePart creates an empty Stream for the new part. The PackagePart.GetStream method can be used to obtain a reference to the stream instance associated with the part.

For additional information, see the Open Packaging Conventions (OPC) specification available for download at http://go.microsoft.com/fwlink/?LinkID=71255.

Notes to Inheritors

CreatePart internally calls the derived class's CreatePartCore method to actually create the part based on the physical format implemented in the derived class.

The following example shows how to create a new PackagePart and then store data into the part. For the complete sample, see Writing a Package Sample.

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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft