OpenWrite Method

FileInfo.OpenWrite Method ()


Creates a write-only FileStream.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public FileStream OpenWrite()

Return Value

Type: System.IO.FileStream

A write-only unshared FileStream object for a new or existing file.

Exception Condition

The path specified when creating an instance of the FileInfo object is read-only or is a directory.


The path specified when creating an instance of the FileInfo object is invalid, such as being on an unmapped drive.

The OpenWrite method opens a file if one already exists for the file path, or creates a new file if one does not exist. For an existing file, it does not append the new text to the existing text. Instead, it overwrites the existing characters with the new characters. If you overwrite a longer string (such as "This is a test of the OpenWrite method") with a shorter string (like "Second run"), the file will contain a mix of the strings ("Second runtest of the OpenWrite method").

The following example opens a file for writing and then reads from the file.

Snippet section 1 of code snippet {"project_id":"3fedad16-eaf1-41a6-8f96-0c1949c68f32","entity_id":"7270839e-e05d-45fc-b9e2-d241acc4c49a","entity_type":"CodeSnippet","locale":"en-US"} in source file ({"filename":"/CS/file openwrite.cs","blob_type":"Source","blob_id":"-002fcs-002ffile-0020openwrite-002ecs","blob_revision":2}), line 1 is missing the closing tag.

for reading and writing files. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft