Export (0) Print
Expand All

FileStream.Write Method

Writes a block of bytes to this stream using data from a buffer.

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

public override void Write (
	byte[] array,
	int offset,
	int count
)
public void Write (
	byte[] array, 
	int offset, 
	int count
)
public override function Write (
	array : byte[], 
	offset : int, 
	count : int
)

Parameters

array

The buffer containing data to write to the stream.

offset

The zero-based byte offset in array at which to begin copying bytes to the current stream.

count

The maximum number of bytes to be written to the current stream.

Exception typeCondition

ArgumentNullException

array is a null reference (Nothing in Visual Basic).

ArgumentException

offset and count describe an invalid range in array.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

- or -

Another thread may have caused an unexpected change in the position of the operating system's file handle.

ObjectDisposedException

The stream is closed.

NotSupportedException

The current stream instance does not support writing.

This method overrides Write.

The offset parameter gives the offset of the byte in array (the buffer index) at which to begin copying, and the count parameter gives the maximum number of bytes that will be written to the stream. If the write operation is successful, the current position of the stream is advanced by the number of bytes written. If an exception occurs, the current position of the stream is unchanged.

NoteNote

Use the CanWrite property to determine whether the current instance supports writing. For additional information, see CanWrite.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file.

File.Move

FileInfo.MoveTo

Copy a file.

File.Copy

FileInfo.CopyTo

Get the size of a file.

FileInfo.Length

Get the attributes of a file.

File.GetAttributes

Set the attributes of a file.

File.SetAttributes

Determine if a file exists.

File.Exists

Read from a binary file.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write to a Newly Created Data File

Create a directory.

Directory.CreateDirectory

Directory.CreateDirectory

This code example is part of a larger example provided for the Lock method.

// Write the original file data.
if(fileStream.Length == 0)
{
    tempString = 
        lastRecordText + recordNumber.ToString();
    fileStream.Write(uniEncoding.GetBytes(tempString), 
        0, uniEncoding.GetByteCount(tempString));
}

// Write the original file data.
if ( fileStream.get_Length() == 0  ) {
    tempString = lastRecordText +(
        new Integer( recordNumber)).ToString();
    fileStream.Write(uniEncoding.GetBytes(tempString),
        0, uniEncoding.GetByteCount(tempString));
}

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft