Package.CreatePart Method (Uri, String, CompressionOption)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

member CreatePart : 
        partUri:Uri *
        contentType:string *
        compressionOption:CompressionOption -> PackagePart


Type: System.Uri

The URI of the new part.

Type: System.String

The content type of the data stream.

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.

Exception Condition

partUri or contentType is null.


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

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.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 3.0
Return to top