FileInfo.Length 属性

获取当前文件的大小。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public ReadOnly Property Length As Long
用法
Dim instance As FileInfo
Dim value As Long

value = instance.Length
public long Length { get; }
public:
property long long Length {
    long long get ();
}
/** @property */
public long get_Length ()
public function get Length () : long

属性值

当前文件的大小。

异常

异常类型 条件

IOException

Refresh 无法更新文件或目录的状态。

FileNotFoundException

文件不存在。

- 或 -

为一个目录调用 Length 属性。

备注

如果包含该文件的文件系统不支持此信息,则此属性值为 空引用(在 Visual Basic 中为 Nothing)。

下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

复制文件。

File.Copy

FileInfo.CopyTo

重命名或移动文件。

File.Move

FileInfo.MoveTo

读取二进制文件。

如何:对新建的数据文件进行读取和写入

写入二进制文件。

如何:对新建的数据文件进行读取和写入

创建子目录。

CreateSubdirectory

查看目录中的文件。

Name

按大小对目录中的文件排序。

GetFileSystemInfos

设置文件属性。

SetAttributes

示例

下面的示例显示指定文件的大小。

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

FileInfo 类
FileInfo 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本