Vb.net find file size9/17/2023 ![]() I was under the impression that the files size on disc is relative to how many clusters it has to allocate for its size. ![]() "Clusters used = Files Byte Length / Cluster Size = " & ClusterSpaceUsed.ToString & " then total clusters used = " & _ĬlustersUsed.ToString & " then total space used on disk " & ChrW(61) & " total clusters used * cluster size which comes out to " & _ RichTextBox1.AppendText("The file " & ChrW(34) & "C:\Users\John\Desktop\OneMegabyteFile.Txt" & ChrW(34) & " size on disk is " & ChrW(34) & _ RichTextBox1.AppendText(" cluster size is " & ClusterSize.ToString & " bytes per cluster." & vbCrLf & vbCrLf)ĬlusterSpaceUsed = ByteLength / ClusterSizeĬlustersUsed = CLng(Math.Ceiling(ClusterSpaceUsed)) RichTextBox1.AppendText("Volume " & Temp.ToString)ĬlusterSize = CLng(queryObj("BlockSize")) RichTextBox1.AppendText("The file " & ChrW(34) & "C:\Users\John\Desktop\OneMegabyteFile.Txt" & ChrW(34) & " is " & ByteLength.ToString & " Bytes in length." & vbCrLf & vbCrLf)ĭim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Volume")įor Each queryObj As ManagementObject In searcher.Get() ' Get block size, allocation unit size, cluster size of disk.ĭim Temp As String = queryObj("Caption").ToString If File.Exists("C:\Users\John\Desktop\OneMegabyteFile.Txt") Thenĭim ByteLength As Long = FileLen("C:\Users\John\Desktop\OneMegabyteFile.Txt") ' Get file length Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click My.("C:\Users\John\Desktop\OneMegabyteFile.Txt", SB.ToString, False) File length is 1 MB, file size on disk is 1,003,520 bytes. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Loadįor i = 1 To 999997 ' This ends up creating a 1 MB length file when it's written to disk, 3 bytes are added by the system apparently. Imports System.Management ' Add ref Assemblies, Framework, System.Management ![]() Therefore the total allocation units, clusters, whatever that are used are known. Using Math.Ceiling on the Double ClusterSpaceUsed raises its value to the next whole number if it has values after the decimal point. I am using Fileinfo.Length() but it is giving me the normal size of file in bytes. My Requirement is to get the file size on disc in bytes. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |