FileInfo.Length Property
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| Refresh cannot update the state of the file or directory. | |
| 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. | |
| Write to a text file. | |
| Read from a text file. | |
| Append text to a file. | |
| Copy a file. | |
| Rename or move a file. | |
| Read from a binary file. | |
| Write to a binary file. | |
| Create a subdirectory. | |
| See the files in a directory. | |
| Sort files in a directory by size. | |
| Set file attributes. |
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.