在 VB.NET 中确定文件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10175630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determining file size in VB.NET
提问by John Wheal
How do I determine the size of a text file?
如何确定文本文件的大小?
I know that I could just count characters, but the file will be several MB's large.
我知道我可以只计算字符数,但文件将有几 MB 大。
回答by Dennis Traub
Dim myFile As New FileInfo("file.txt")
Dim sizeInBytes As Long = myFile.Length
回答by Slai
For anyone looking for the shorter VB version:
对于任何寻找更短的 VB 版本的人:
FileLen("file.txt")
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.filelen
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.filelen
回答by Nando Style
The use of filecan be dangerous as it is also the name of a class.
It is better to code it as follows:
使用file可能很危险,因为它也是类的名称。最好按如下方式编码:
Dim myFile As New FileInfo("file.txt")
Dim sizeInBytes As Long = myFile.Length
回答by Mubarak
The code from the other answer does not check the correct size of the file:
另一个答案中的代码不会检查文件的正确大小:
Dim myFile As New FileInfo("file.txt")
Dim sizeInBytes As Long = MyFile.Length
Try this code instead
试试这个代码
Dim infoReader As System.IO.FileInfo = _
My.Computer.FileSystem.GetFileInfo("C:\testfile.txt")
MsgBox("File C:\testfile.txt is " & infoReader.Length & " bytes.")
It is from How to: Determine a File's Size in Visual Basic(MSDN).
它来自如何:在 Visual Basic(MSDN) 中确定文件的大小。

