开发者

File size in vb.net

In Form1 I have a Textbox1, in this textbox I have the location of a file "C:\folder\file.iso"

In the Form2 I want to get the file size of the file in Textbox1 so I tried this

        Dim fileDetail As IO.FileInfo


    fileDetai开发者_StackOverflow中文版l = My.Computer.FileSystem.GetFileInfo(Form1.Textbox1.Text)

    Label1.Text = Size: fileDetail.Length
End Sub

I dont get an error, but the size of the file isn't showed in the label.

Edit: This doesn't seem to work

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

It still doesn't give me the size of the file nor it gives the "Size:"


    Dim fileDetail = My.Computer.FileSystem.GetFileInfo(form1.Textbox1.Text)
    Label1.Text = "Size : " & fileDetail.Length


' 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


Code works perfect but something in my project is blocking it.

Created a new project and it worked perfect.


'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

There is two choice, Which you want

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜