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.EndWrite Method

Ends an asynchronous write, blocking until the I/O operation has completed.

[Visual Basic]
Overrides Public Sub EndWrite( _
   ByVal asyncResult As IAsyncResult _
)
[C#]
public override void EndWrite(
 IAsyncResult asyncResult
);
[C++]
public: void EndWrite(
 IAsyncResult* asyncResult
);
[JScript]
public override function EndWrite(
   asyncResult : IAsyncResult
);

Parameters

asyncResult
The pending asynchronous I/O request.

Exceptions

Exception Type Condition
ArgumentNullException asyncResult is a null reference (Nothing in Visual Basic).
ArgumentException This IAsyncResult object was not created by calling BeginWrite on this class.
InvalidOperationException EndWrite is called multiple times.

Remarks

This method overrides EndWrite.

EndWrite must be called exactly once on every IAsyncResult from BeginWrite. EndWrite will block until the I/O operation has completed.

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 System.IO.FileStream.FileStream4.

[Visual Basic] 
Private Shared Sub EndWriteCallback(asyncResult As IAsyncResult)
    Dim tempState As State = _
        DirectCast(asyncResult.AsyncState, State)
    Dim fStream As FileStream = tempState.FStream
    fStream.EndWrite(asyncResult)

    ' Asynchronously read back the written data.
    fStream.Position = 0
    asyncResult = fStream.BeginRead( _ 
        tempState.ReadArray, 0 , tempState.ReadArray.Length, _
        AddressOf EndReadCallback, tempState)

    ' Concurrently do other work, such as 
    ' logging the write operation.
End Sub

[C#] 
static void EndWriteCallback(IAsyncResult asyncResult)
{
    State tempState = (State)asyncResult.AsyncState;
    FileStream fStream = tempState.FStream;
    fStream.EndWrite(asyncResult);

    // Asynchronously read back the written data.
    fStream.Position = 0;
    asyncResult = fStream.BeginRead(
        tempState.ReadArray, 0 , tempState.ReadArray.Length, 
        new AsyncCallback(EndReadCallback), tempState);

    // Concurrently do other work, such as 
    // logging the write operation.
}

[C++] 
static void EndWriteCallback(IAsyncResult* asyncResult)
{
    State* tempState = 
        dynamic_cast<State*>(asyncResult->AsyncState);
    FileStream* fStream = tempState->FStream;
    fStream->EndWrite(asyncResult);

    // Asynchronously read back the written data.
    fStream->Position = 0;
    asyncResult = fStream->BeginRead(
        tempState->ReadArray, 0 , tempState->ReadArray->Length, 
        new AsyncCallback(
        0, &FStream::EndReadCallback), tempState);

    // Concurrently do other work, such as 
    // logging the write operation.
}
};

[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 | Asynchronous File I/O

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft