在Form1中我有一个Textbox1,在这个文本框中我有一个文件“C:\ folder \ file.iso”的位置
在Form2中,我想在Textbox1中获取文件的文件大小,所以我尝试了这个
Dim fileDetail As IO.FileInfo
fileDetail = My.Computer.FileSystem.GetFileInfo(Form1.Textbox1.Text)
Label1.Text = Size: fileDetail.Length
End Sub
我没有收到错误,但标签中没有显示文件的大小。
编辑:这似乎不起作用
Private Sub Unscramble_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If System.IO.File.Exists(Form1.TextBox2.Text) Then
Dim fi As New System.IO.FileInfo(Form1.TextBox2.Text)
Label3.Text = "Size: " & fi.Length.ToString()
End If
End Sub
它仍然没有给我文件的大小,也没有给出“大小:”
答案 0 :(得分:3)
Dim fileDetail = My.Computer.FileSystem.GetFileInfo(form1.Textbox1.Text)
Label1.Text = "Size : " & fileDetail.Length
答案 1 :(得分:2)
' this is the first(main) form'
Public Class Form1
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' create the form2 by PASSING it the file path in constructor'
Dim f2 As New Form2(TextBox1.Text)
f2.ShowDialog()
End Sub
End Class
' this is the second form'
Public Class Form2
Inherits Form
Private _filePath As String
Private Label1 As Label
Public Sub New(ByVal filePath As String)
_filePath = filePath
End Sub
' this is the _Load method of the second form'
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
If IO.File.Exists(_filePath) Then
Dim fi As New IO.FileInfo(_filePath)
Label1.Text = "Size :" & fi.Length.ToString()
End If
End Sub
End Class
答案 2 :(得分:0)
代码完美无缺,但我项目中的某些内容阻止了它。
创建了一个新项目,它运作得很完美。
答案 3 :(得分:0)
'label3.Text is my all string with file size.
Label3.Text = "Size : " & My.Computer.FileSystem.GetFileInfo("C:\Download\my song.mp3").Length & " Bytes"
'Output: Size: 2344 Bytes
Label3.Text = "Size : " & System.Math.Round(My.Computer.FileSystem.GetFileInfo("C:\Download\my song.mp3").Length / 1024) & " KB"
'Output: Size: 2 KB
有两种选择,你想要的