Export (0) Print
Expand All

FileStream.Close Method

.NET Framework 1.1

Closes the file and releases any resources associated with the current file stream.

[Visual Basic]
Overrides Public Sub Close()
[C#]
public override void Close();
[C++]
public: void Close();
[JScript]
public override function Close();

Exceptions

Exception Type Condition
IOException An error occurred while trying to close the stream.

Remarks

This method overrides Close.

This implementation of Close calls the Dispose method passing a true value.

Any data previously written to the buffer is copied to the file before the file stream is closed, so it is not necessary to call Flush before invoking Close. Following a call to Close, any operations on the file stream might raise exceptions. After Close has been called once, it does nothing if called again.

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close. Setting AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

The Finalize method invokes Close so that the file stream is closed before the garbage collector finalizes the object. However, objects writing to the FileStream, such as a StreamWriter, might not have flushed the data from their internal buffers to the FileStream when the call to Finalize closes the stream. To prevent data loss, always call Close on the highest-level object.

If a process terminates with part of a file locked or closes a file that has outstanding locks, the behavior is undefined.

Note   This version of Close is equivalent to Dispose with the disposing parameter set to true.

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 EndReadCallback(asyncResult As IAsyncResult)
     Dim tempState As State = _
         DirectCast(asyncResult.AsyncState, State)
     Dim readCount As Integer = _
         tempState.FStream.EndRead(asyncResult)

     Dim i As Integer = 0
     While(i < readCount)
         If(tempState.ReadArray(i) <> tempState.WriteArray(i))
             Console.WriteLine("Error writing data.")
             tempState.FStream.Close()
             Return
         End If
         i += 1
     End While

     Console.WriteLine("The data was written to {0} and " & _
         "verified.", tempState.FStream.Name)
     tempState.FStream.Close()

     ' Signal the main thread that the verification is finished.
     tempState.ManualEvent.Set()
 End Sub

[C#] 
static void EndReadCallback(IAsyncResult asyncResult)
{
    State tempState = (State)asyncResult.AsyncState;
    int readCount = tempState.FStream.EndRead(asyncResult);

    int i = 0;
    while(i < readCount)
    {
        if(tempState.ReadArray[i] != tempState.WriteArray[i++])
        {
            Console.WriteLine("Error writing data.");
            tempState.FStream.Close();
            return;
        }
    }
    Console.WriteLine("The data was written to {0} and verified.",
        tempState.FStream.Name);
    tempState.FStream.Close();

    // Signal the main thread that the verification is finished.
    tempState.ManualEvent.Set();
}

[C++] 
static void EndReadCallback(IAsyncResult* asyncResult)
{
    State* tempState = 
        dynamic_cast<State*>(asyncResult->AsyncState);
    int readCount = tempState->FStream->EndRead(asyncResult);

    int i = 0;
    while(i < readCount)
    {
        if(tempState->ReadArray[i] != tempState->WriteArray[i++])
        {
            Console::WriteLine(S"Error writing data.");
            tempState->FStream->Close();
            return;
        }
    }
    Console::WriteLine(S"The data was written to {0} "
        S"and verified.", tempState->FStream->Name);
    tempState->FStream->Close();

    // Signal the main thread that the verification is finished.
    tempState->ManualEvent->Set();
}

[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:
© 2014 Microsoft