This documentation is archived and is not being maintained.

StreamWriter.Write Method (Char[], Int32, Int32)

.NET Framework 1.1

Writes a subarray of characters to the stream.

[Visual Basic]
Overrides Overloads Public Sub Write( _
   ByVal buffer() As Char, _
   ByVal index As Integer, _
   ByVal count As Integer _
public override void Write(
 char[] buffer,
 int index,
 int count
public: void Write(
 __wchar_t buffer __gc[],
 int index,
 int count
public override function Write(
   buffer : Char[],
 index : int,
 count : int


A character array containing the data to write.
The index into buffer at which to begin writing.
The number of characters to read from buffer.


Exception Type Condition
ArgumentNullException buffer is a null reference (Nothing in Visual Basic).
ArgumentException The buffer length minus index is less than count.
ArgumentOutOfRangeException index or count is negative.
IOException An I/O error occurs.
ObjectDisposedException AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.
NotSupportedException AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.


This method overrides TextWriter.Write.

The characters are read from buffer beginning at index and continuing through index + (count- 1). All characters are written to the underlying stream unless the end of the underlying stream is reached prematurely. Flush is invoked automatically if AutoFlush is true.

For an example of using this method, see the Example section below. 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



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


[Visual Basic, C#, C++] This example writes eight characters from a 13-element array to a file, beginning at the third element of the array.

[Visual Basic] 
Imports System
Imports System.IO

Public Class SWBuff

    Public Shared Sub Main()
        Dim sb As New FileStream("MyFile.txt", FileMode.OpenOrCreate)
        Dim b As Char() = {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, _
           "h"c, "i"c, "j"c, "k"c, "l"c, "m"c}
        Dim sw As New StreamWriter(sb)
        sw.Write(b, 3, 8)
    End Sub
End Class

using System;
using System.IO;
public class SWBuff 
    public static void Main(String[] args)
        FileStream sb = new FileStream("MyFile.txt", FileMode.OpenOrCreate);
        char[] b = {'a','b','c','d','e','f','g','h','i','j','k','l','m'};
        StreamWriter sw = new StreamWriter(sb);
        sw.Write(b, 3, 8);


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


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

StreamWriter Class | StreamWriter Members | System.IO Namespace | StreamWriter.Write Overload List | Working with I/O | Reading Text from a File | Writing Text to a File