窗体上添加一个按钮,代码如下:
为从化等地区用户提供了全套网页设计制作服务,及从化网站建设行业解决方案。主营业务为成都网站建设、成都网站制作、从化网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Text = "稍等!"
Button1.Enabled = False
Dim MyFolder As String = "C:\Windows"
Dim MyDir As New System.IO.DirectoryInfo(MyFolder)
Dim MyInfo As String = MyFolder + "文件夹的大小是:" + CalculateDirectorySize(MyDir, True).ToString() + "字节。"
MessageBox.Show(MyInfo, "信息提示", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Button1.Text = "开始"
Button1.Enabled = True
End Sub
Public Shared Function CalculateDirectorySize(ByVal MyDirectory As System.IO.DirectoryInfo, ByVal IsSubDirectories As Boolean) As Long
Dim MySize As Long = 0
'检查包含的所有文件
Dim MyFiles() As System.IO.FileInfo = MyDirectory.GetFiles()
For Each MyFile As System.IO.FileInfo In MyFiles
MySize += MyFile.Length
Next
'检查包含的所有子文件夹
If (IsSubDirectories) Then
Dim MyDirs() As System.IO.DirectoryInfo = MyDirectory.GetDirectories()
For Each MyDir As System.IO.DirectoryInfo In MyDirs
MySize += CalculateDirectorySize(MyDir, True)
Next
End If
Return MySize
End Function
End Class
你实例化一个文件对象,就可以获取这个文件的相关属性了,VB.NET实例,以你的文件为例:
Dim sourcefileinfo As New FileInfo(“ c:\1.txt”) '定义并实例化FileInfo对象。length就是它的大小!
label1.Text = "文件大小为: " sourcefileinfo.Length "字节"
首先引入System.IO命名空间(在代码文件的最上方)
Imports System.IO
然后在需要用到的地方:
Dim mp3fileinfo As New FileInfo(mp3文件完整路径字符串)
'mp3fileinfo.Length就是文件的大小,以B(字节)为单位,除以1024得到KB,再除以1024得到MB