FileInfo::Length Property

Gets the size, in bytes, of the current file.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public:
property long long Length {
	long long get ();
}

Property Value

Type: System::Int64
The size of the current file in bytes.

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

The value of the Length property is pre-cached if the current instance of the FileInfo object was returned from any of the following DirectoryInfomethods:

To get the latest value, call the Refresh method.

The following example displays the size of the specified files.

// The following example displays the names and sizes 
// of the files in the specified directory. 
using namespace System;
using namespace System::IO;
int main()
{

   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\" );

   // Get a reference to each file in that directory. 
   array<FileInfo^>^fiArr = di->GetFiles();

   // Display the names and sizes of the files.
   Console::WriteLine( "The directory {0} contains the following files:", di->Name );
   System::Collections::IEnumerator^ myEnum = fiArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      FileInfo^ f = safe_cast<FileInfo^>(myEnum->Current);
      Console::WriteLine( "The size of {0} is {1} bytes.", f->Name, f->Length );
   }
}
//This code produces output similar to the following;  
//results may vary based on the computer/file structure/etc.: 
// 
//The directory c:\ contains the following files: 
//The size of MyComputer.log is 274 bytes. 
//The size of AUTOEXEC.BAT is 0 bytes. 
//The size of boot.ini is 211 bytes. 
//The size of CONFIG.SYS is 0 bytes. 
//The size of hiberfil.sys is 1072775168 bytes. 
//The size of IO.SYS is 0 bytes. 
//The size of MASK.txt is 2700 bytes. 
//The size of mfc80.dll is 1093632 bytes. 
//The size of mfc80u.dll is 1079808 bytes. 
//The size of MSDOS.SYS is 0 bytes. 
//The size of NTDETECT.COM is 47564 bytes. 
//The size of ntldr is 250032 bytes. 
//The size of pagefile.sys is 1610612736 bytes. 
//The size of UpdatePatch.log is 22778 bytes. 
//The size of UpdatePatch.txt is 30 bytes. 
//The size of wt3d.ini is 234 bytes.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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