Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Package.CreatePart Method (Uri, String)

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

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

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



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


The content type of the data stream.

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

The part CompressionOption is 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 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

Community Additions

© 2015 Microsoft