منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : كود جلب حجم الملف
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
بسم الله الرحمان الرحيم

السلام عليكم 


مثل ما موضح في العنوان اريد كود جلب حجم الملف ويضع حجم الملف في 


الليبل بس


وشكرا
شوف اخي حظر 2 من  button
+ TextBox1.Text
 +lable1

button1
كود :
  Dim i As New FolderBrowserDialog
       If i.ShowDialog Then
           TextBox1.Text = i.SelectedPath ' هنا لاستعارض الملف الذي تود اظهاره
       End If

butto2
كود :
 Dim driveName As String = TextBox1.Text ' عبارة عن تعريف يمكنك تغير التكست بوكس بالكومبكس او اي شيئ من نوع string
       Dim drive As New DriveInfo(driveName)
       Label1.Text = "Total Size: " + Format(drive.TotalSize / 1024 / 1024 / 1024) + "GB" 'اظهار حجم الملف باجيجا بايت


وهاهو الكود باكامل

كود :
Imports System.IO

Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim i As New FolderBrowserDialog
       If i.ShowDialog Then
           TextBox1.Text = i.SelectedPath ' هنا لاستعارض الملف الذي تود اظهاره
       End If
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim driveName As String = TextBox1.Text ' عبارة عن تعريف يمكنك تغير التكست بوكس بالكومبكس او اي شيئ من نوع string
       Dim drive As New DriveInfo(driveName)
       Label1.Text = "Total Size: " + Format(drive.TotalSize / 1024 / 1024 / 1024) + "GB" 'اظهار حجم الملف باجيجا بايت

   End Sub
End Class

ارجو ان يكون المطلوب
او تفضل هذا يعطيك الحجم الذي تريد

تحتتاج لثلاثة من الليبل lebal1 label2 label3

كود :
Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As New FolderBrowserDialog
        If i.ShowDialog Then
            TextBox1.Text = i.SelectedPath ' هنا لاستعارض الملف الذي تود اظهاره
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dInfo As New DirectoryInfo(TextBox1.Text)
        ' set bool parameter to false if you
        ' do not want to include subdirectories.
        Dim sizeOfDir As Long = DirectorySize(dInfo, True)

        Label1.Text = ("Directory size in Bytes : " & "{0:N0} Bytes" & sizeOfDir)
        Label2.Text = ("Directory size in KB : " & "{0:N2} KB" & (CDbl(sizeOfDir)) / 1024)
        Label3.Text = ("Directory size in MB : " & "{0:N2} MB" & (CDbl(sizeOfDir)) / (1024 * 1024))

    End Sub
    Private Function DirectorySize(ByVal dInfo As DirectoryInfo, _
   ByVal includeSubDir As Boolean) As Long
        ' Enumerate all the files
        Dim totalSize As Long = dInfo.EnumerateFiles() _
          .Sum(Function(file) file.Length)

        ' If Subdirectories are to be included
        If includeSubDir Then
            ' Enumerate all sub-directories
            totalSize += dInfo.EnumerateDirectories() _
             .Sum(Function(dir) DirectorySize(dir, True))
        End If
        Return totalSize
    End Function
End Class

احذف lebal الذي تريد Big Grin
تفضل الدالتين واحدة للميجابايت والاخرى للكيلو والفرق بسيط
كود :
Private Function gmbfs(ByVal pth As String) As Single
       Dim fs As Single = My.Computer.FileSystem.GetFileInfo(pth).Length
       Return CInt(fs / 1024) / (1024)
   End Function
كود :
Private Function gkbfs(ByVal pth As String) As Single
       Dim fs As Single = My.Computer.FileSystem.GetFileInfo(pth).Length
       Return CInt(fs / 1024)
   End Function
للاستخدام
كود :
       Dim ofd As New OpenFileDialog
       If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
           Label1.Text = gkbfs(ofd.FileName) & "Size: kb" & vbNewLine & gmbfs(ofd.FileName) & "Size: mb"
       End If
الله يعطيك العافيه جميعا