通过 System.IO.File 类中的 GetLength 方法来实现的。这个方法返回文件的大小(以字节为单位)。
在 VB.NET 中,你可以这样使用 GetLength 方法:
vbnet复制代码
Imports System.IO | |
Module Module1 | |
Sub Main() | |
Dim filePath As String = "path\to\your\file.txt" | |
Dim fileLength As Long = File.GetLength(filePath) | |
Console.WriteLine("File length: " & fileLength & " bytes") | |
End Sub | |
End Module |
在 C# 中,你可以这样使用 GetLength 方法:
csharp复制代码
using System; | |
using System.IO; | |
class Program | |
{ | |
static void Main() | |
{ | |
string filePath = "path\\to\\your\\file.txt"; | |
long fileLength = File.GetLength(filePath); | |
Console.WriteLine("File length: " + fileLength + " bytes"); | |
} | |
} |
在这两个示例中,File.GetLength 方法用于获取指定文件的大小,并将结果存储在 fileLength 变量中。然后,程序将文件大小打印到控制台。
请确保将 filePath 替换为你要检查的实际文件的路径。如果文件不存在,GetLength 方法将抛出一个异常,因此你可能需要添加错误处理逻辑来处理这种情况。
本文介绍了如何在VB.NET和C#中利用System.IO.File.GetLength方法获取文件的字节大小,并提到了需要处理文件不存在时的异常情况。
1334

被折叠的 条评论
为什么被折叠?



