Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileStream.Write Method

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

[Visual Basic]
Overrides Public Sub Write( _
   ByVal array() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
)
[C#]
public override void Write(
 byte[] array,
 int offset,
 int count
);
[C++]
public: void Write(
 unsigned char array __gc[],
 int offset,
 int count
);
[JScript]
public override function Write(
   array : Byte[],
 offset : int,
 count : int
);

Parameters

array
The array to which bytes are written.
offset
The byte offset in array at which to begin writing.
count
The maximum number of bytes to write.

Exceptions

Exception Type Condition
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.
ObjectDisposedException The stream is closed.
NotSupportedException The current stream instance does not support writing.

Remarks

This method overrides Write.

The offset parameter gives the offset of the byte in array (the buffer index) at which to begin writing, and the count parameter gives the maximum number of bytes that will be written to this 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.

Note   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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending 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. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
Create a directory. Directory.CreateDirectory

Directory.CreateDirectory

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for the Lock method.

[Visual Basic] 
' Write the original file data.
If aFileStream.Length = 0 Then
    tempString = _
        lastRecordText + recordNumber.ToString()
    aFileStream.Write(uniEncoding.GetBytes(tempString), _
        0, uniEncoding.GetByteCount(tempString))
End If

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

[C++] 
// Write the original file data.
if(fileStream->Length == 0)
{
    tempString = String::Concat(
        lastRecordText, recordNumber.ToString());
    fileStream->Write(uniEncoding->GetBytes(tempString), 
        0, uniEncoding->GetByteCount(tempString));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

FileStream Class | FileStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2017 Microsoft