Export (0) Print
Expand All

Package.CreatePart Method (Uri, String)

Creates a new uncompressed part with a given URI and content type.

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

public PackagePart CreatePart(
	Uri partUri,
	string contentType


Type: System.Uri

The uniform resource identifier (URI) of the new part.

Type: System.String

The content type of the data stream.

Return Value

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


partUri or contentType is null.


partUri is not a valid PackagePart URI.


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


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


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

CreatePart initializes 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.

The part CompressionOption is CompressionOption.NotCompressed.

For additional information about package parts, see section 1.1 in 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 =

// 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
© 2015 Microsoft