FileInfo.Length Property
Gets the size of the current file.
[Visual Basic] Public ReadOnly Property Length As Long [C#] public long Length {get;} [C++] public: __property __int64 get_Length(); [JScript] public function get Length() : long;
Property Value
The size of the current file.
Exceptions
| Exception Type | Condition |
|---|---|
| IOException | Refresh cannot update the state of the file or directory. |
| FileNotFoundException | The file does not exist.
-or- The Length property is called for a directory. |
Remarks
This property value is a null reference (Nothing in Visual Basic) if the file system containing the file does not support this information.
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 |
| Copy a file. | File.Copy |
| Rename or move a file. | File.Move |
| 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 subdirectory. | CreateSubdirectory |
| See the files in a directory. | Name |
| Sort files in a directory by size. | GetFileSystemInfos |
| Set file attributes. | SetAttributes |
Example
[Visual Basic, C#, C++] The following example displays the size of the specified files.
[Visual Basic] ' The following example displays the names and sizes ' of the files in the specified directory. Imports System Imports System.IO Public Class FileLength Public Shared Sub Main() ' Make a reference to a directory. Dim di As New DirectoryInfo("c:\") ' Get a reference to each file in that directory. Dim fiArr As FileInfo() = di.GetFiles() ' Display the names and sizes of the files. Dim f As FileInfo Console.WriteLine("The directory {0} contains the following files:", di.Name) For Each f In fiArr Console.WriteLine("The size of {0} is {1} bytes.", f.Name, f.Length) Next f End Sub 'Main End Class 'FileLength [C#] // The following example displays the names and sizes // of the files in the specified directory. using System; using System.IO; public class FileLength { public static void Main() { // Make a reference to a directory. DirectoryInfo di = new DirectoryInfo("c:\\"); // Get a reference to each file in that directory. FileInfo[] fiArr = di.GetFiles(); // Display the names and sizes of the files. Console.WriteLine("The directory {0} contains the following files:", di.Name); foreach (FileInfo f in fiArr) Console.WriteLine("The size of {0} is {1} bytes.", f.Name, f.Length); } } [C++] // The following example displays the names and sizes // of the files in the specified directory. #using <mscorlib.dll> using namespace System; using namespace System::IO; int main() { // Make a reference to a directory. DirectoryInfo* di = new DirectoryInfo(S"c:\\"); // Get a reference to each file in that directory. FileInfo* fiArr[] = di->GetFiles(); // Display the names and sizes of the files. Console::WriteLine(S"The directory {0} contains the following files:", di->Name); System::Collections::IEnumerator* myEnum = fiArr->GetEnumerator(); while (myEnum->MoveNext()) { FileInfo* f = __try_cast<FileInfo*>(myEnum->Current); Console::WriteLine(S"The size of {0} is {1} bytes.", f->Name, __box(f->Length)); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
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
See Also
FileInfo Class | FileInfo Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File