Export (0) Print
Expand All

FileInfo.Length Property

Gets the size of the current file.

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

public:
property long long Length {
	long long get ();
}
/** @property */
public long get_Length ()

public function get Length () : long

Property Value

The size of the current file.

Exception typeCondition

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.

This property value is a null reference (Nothing in Visual Basic) if the file system containing the file does not support this information.

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.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText

Copy a file.

File.Copy

FileInfo.CopyTo

Rename or move a file.

File.Move

FileInfo.MoveTo

Read from a binary file.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write 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

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 );
   }
}


// The following example displays the names and sizes
// of the files in the specified directory.
import System.*;
import System.IO.*;

public class FileLength
{
    public static void main(String[] args)
    {
        // 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.get_Name());
        for (int iCtr = 0; iCtr < fiArr.length; iCtr++) {
            FileInfo f = fiArr[iCtr];
            Console.WriteLine("The size of {0} is {1} bytes.", f.get_Name(),
                System.Convert.ToString(f.get_Length()));
        }
    } //main
} //FileLength

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft