FileInfo.Length 屬性

定義

取得目前檔案的大小,以位元組為單位。

public:
 property long Length { long get(); };
public long Length { get; }
member this.Length : int64
Public ReadOnly Property Length As Long

屬性值

目前檔案的大小,以位元組為單位。

例外狀況

Refresh() 無法更新檔案或目錄的狀態。

檔案不存在。

-或-

已針對目錄呼叫 Length 屬性。

範例

下列範例會顯示指定檔案的大小。

// 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.
// 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);
    }
}
//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.
' The following example displays the names and sizes
' of the files in the specified directory.
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
End Class

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

備註

如果物件的目前實例 FileInfo 從下列 DirectoryInfo 任一方法傳回,則會預先快取 屬性的值 Length

若要取得最新的值,請呼叫 Refresh 方法。

適用於

另請參閱