Monday, August 27, 2012

Get Size of file on disk C#


fileName = @"C:\Program Files\CLIRetail.log";
FileInfo f = new FileInfo(fileName);
long s1 = f.Length;
string size = GetFileSize(s1);
Console.WriteLine("Size : " + size);


public static string GetFileSize(long Bytes) {
if (Bytes >= 1073741824) {
Decimal size = Decimal.Divide(Bytes, 1073741824);
return String.Format("{0:##.##} GB", size);
}
else if (Bytes >= 1048576)
{
Decimal size = Decimal.Divide(Bytes, 1048576);
return String.Format("{0:##.##} MB", size);
}
else if (Bytes >= 1024)
{
Decimal size = Decimal.Divide(Bytes, 1024);
return String.Format("{0:##.##} KB", size);
}
else if (Bytes > 0 & Bytes < 1024)
{
Decimal size = Bytes;
return String.Format("{0:##.##} Bytes", size);
}
else
{
return "0 Bytes";
}

2 comments:

  1. Greetings, Ranganayaki;

    The C# code fragment above returns the actual logical length or size of a file when loaded in memory, not the amount of disk space when a file is saved. The reason that these two values, logical length and size on disk for a file are not the same has to do with how the Windows File System works.

    Essentially, when space is allocated to store a file on a disk, the Windows OS does not and cannot get the exact number of bytes that reflect the logical length or number of bytes in the file when loaded into memory. To store a file on the disk, the Windows OS must obtain a set of clusters available on a drive that is large enough to contain the logical contents of a file. When the file is then written out to the drive, regardless of the type of file system used to format the drive or device, such as NTFS or FAT32, the total number of bytes required to save a file to disk depends on the number of clusters the OS must use on the drive to save the entire file.

    The Windows OS can only work with reading and writing files from and to a disk drive by using clusters. As a result, the total number of bytes needed to save a file on disk will be equal to or, more likely, greater than the size or length of the file in memory as returned by the .NET System.IO.FileInfo() class's Length property.

    For example, when I look at an MP3 file on an NTFS formatted portable hard disk drive I have, named Rodriguez_03_I Think of You.mp3, the Properties dialog from Windows Explorer indicates that this file has a Size of 5.15MB (5,406,843 bytes), and a Size on disk of 5.15MB (5,407,744 bytes). To see how the portable hard disk drive, mounted with the logical drive letter F: is formatted, I can run the FSUtil.Exe command from either the Command Prompt (CMD.Exe) or PowerShell:

    PS C:\> \windows\system32\fsutil fsinfo ntfsinfo f:
    NTFS Volume Serial Number : 0xdccc3b1acc3aee80
    Version : 3.1
    Number Sectors : 0x000000003a384c01
    Total Clusters : 0x000000001d1c2600
    Free Clusters : 0x00000000156a4d6c
    Total Reserved : 0x0000000000001120
    Bytes Per Sector : 512
    Bytes Per Physical Sector :
    Bytes Per Cluster : 1024
    Bytes Per FileRecord Segment : 1024
    Clusters Per FileRecord Segment : 1
    Mft Valid Data Length : 0x00000000027cc000
    Mft Start Lcn : 0x0000000000300000
    Mft2 Start Lcn : 0x000000000e8e1300
    Mft Zone Start : 0x0000000000309f20
    Mft Zone End : 0x0000000000336400
    RM Identifier: 8AA3CBA8-DEE8-11DF-8635-485B391421D4
    PS C:\>

    FSUtil.Exe shows that each cluster each 1024 Bytes or 1Kb in length. So, if I divide the total number of Bytes reported for this MP3 file in the Size on Disk section of the Windows Explorer Properties dialog, 5407744 (the bytes on disk the file takes up) by 1Kb or 1024, I now know that 5281 clusters are needed to store this file. Going backwards from what the FileInfo.Length property reports, that the MP3 file is 5406843 bytes, that when divided by the disk drive's NTFS Cluster Size of 1024, results in at least 5280 1Kb clusters on the F: drive to be set aside to save this file. When the 5280 1Kb or 1024 byte clusters is multiplied, it comes out to 5406720, which is 123 bytes less shorter than having the entire MP3 file loaded into memory. To make sure that the contents of the entire file are save and loaded again consistently and correctly, the Windows OS needs to set aside another 1024 byte cluster to hold the remaining 123 bytes.

    If you need to determine the actual number of bytes that are set aside to store a file on a disk, the following disscussion thread on stackoverflow, "Get Size of file on disk" at http://stackoverflow.com/questions/3750590/get-size-of-file-on-disk may have exactly what you're looking for.

    I hope that this was of help and interest.

    ReplyDelete