Export (0) Print
Expand All

FileStream.Length Property

Gets the length in bytes of the stream.

[Visual Basic]
Overrides Public ReadOnly Property Length As Long
[C#]
public override long Length {get;}
[C++]
public: __property __int64 get_Length();
[JScript]
public override function get Length() : long;

Property Value

A long value representing the length of the stream in bytes.

Exceptions

Exception Type Condition
NotSupportedException CanSeek for this stream is false.
IOException An I/O error occurs, such as the file being closed.

Remarks

For an example of using this property, 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

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 directory. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Create a subdirectory. CreateSubdirectory
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
See the files in a directory. Name
Sort files in a directory by size. GetFileSystemInfos

Example

The following example uses the Length and Position properties to check for an end-of-file condition.

[Visual Basic] 
If s.Length = s.Position Then
    Console.WriteLine("End of file has been reached.")
End If

[C#] 
if( s.Length==s.Position )
{
   Console.WriteLine("End of file has been reached.");
}

[C++] 
if( s->Length == s->Position )
{
    Console::WriteLine(S"End of file has been reached.");
}

[JScript] 
if( s.Length==s.Position )
    Console.WriteLine("End of file has been reached.");

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