This documentation is archived and is not being maintained.

Package.CreatePart Method (Uri, String, CompressionOption)

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

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

public PackagePart CreatePart (
	Uri partUri,
	string contentType,
	CompressionOption compressionOption
public PackagePart CreatePart (
	Uri partUri, 
	String contentType, 
	CompressionOption compressionOption
public function CreatePart (
	partUri : Uri, 
	contentType : String, 
	compressionOption : CompressionOption
) : PackagePart
You cannot use methods in XAML.



The URI of the new part.


The content type of the data stream.


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

Return Value

The new created part.

Exception typeCondition


partUri or contentType is a null reference (Nothing in Visual Basic).


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


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


The compressionOption value is not valid.


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


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

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

CreatePart instantiates an empty Stream for the new part. The 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

Notes to Implementers: CreatePart internally calls the derived class CreatePartCore method to actually create the part based on the physical format implemented in the subclass.

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 =

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0