Get Size of File on Disk

Get size of file on disk

This uses GetCompressedFileSize, as ho1 suggested, as well as GetDiskFreeSpace, as PaulStack
suggested, it does, however, use P/Invoke. I have tested it only for compressed files, and I suspect it does not work for fragmented files.

public static long GetFileSizeOnDisk(string file)
{
FileInfo info = new FileInfo(file);
uint dummy, sectorsPerCluster, bytesPerSector;
int result = GetDiskFreeSpaceW(info.Directory.Root.FullName, out sectorsPerCluster, out bytesPerSector, out dummy, out dummy);
if (result == 0) throw new Win32Exception();
uint clusterSize = sectorsPerCluster * bytesPerSector;
uint hosize;
uint losize = GetCompressedFileSizeW(file, out hosize);
long size;
size = (long)hosize << 32 | losize;
return ((size + clusterSize - 1) / clusterSize) * clusterSize;
}

[DllImport("kernel32.dll")]
static extern uint GetCompressedFileSizeW([In, MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
[Out, MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);

[DllImport("kernel32.dll", SetLastError = true, PreserveSig = true)]
static extern int GetDiskFreeSpaceW([In, MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName,
out uint lpSectorsPerCluster, out uint lpBytesPerSector, out uint lpNumberOfFreeClusters,
out uint lpTotalNumberOfClusters);

Get file size on disk from file size

Your file length is 0x31132B.

The required storage (rounded up to the nearest cluster) is 0x312000. Your clusters are either 4kB (0x1000) or 8kB (0x2000).

This can be computed as:

clusterSize * ceil(fileSize * 1.0 / clusterSize)

(The 1.0 prevents integer division.) In integer math, it is:

clusterSize * (1 + (fileSize - 1) / clusterSize)

You get the cluster size from GetDiskFreeSpace, which you'll need to call anyway to figure out if your file will fit. See this existing answer:

  • Getting the cluster size of a hard drive (through code)

Of course, other things can affect the true storage used by storing a file... if a directory doesn't have enough space in its cluster for the new entry, if you are storing metadata with it that doesn't fit in the directory, if you have compression enabled. But for an "ordinary" file system, the above calculations will be correct.

Get size and size on disk of files

You could do some math tricks. According to this comment from here by DelphicOracle you could calculate it based on your drive's allocation unit size.

function Get-SizeOnDisk {
[CmdletBinding()]
[OutputType([int64])]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[string]
$Path
)

process {
$absolutePath = ($Path | Resolve-Path).Path
$item = Get-Item $absolutePath
$volume = Get-Volume $item.PSDrive.Name

$d = [int][System.Math]::Ceiling($item.Length / $volume.AllocationUnitSize)
return $d * $volume.AllocationUnitSize
}
}

'myItem.ext' | Get-SizeOnDisk

Basically, you check how many times the Drive's AllocationUnitSize fits in the actual length of the file, and if the division has a rest, you ceil it.

How to check logical and physical file size on disk using C# file API

(new FileInfo(path).Length)

is the actual size.
As for size on disk, I don't think there's an API to get it, but you can get it using the actual size, and the cluster size.

There's some info on the calculation required here:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/85bf76ac-a254-41d4-a3d7-e7803c8d9bc3

linux command to get size of files and directories present in a particular folder?

Use ls command for files and du command for directories.

Checking File Sizes

ls -l filename   #Displays Size of the specified file
ls -l * #Displays Size of All the files in the current directory
ls -al * #Displays Size of All the files including hidden files in the current directory
ls -al dir/ #Displays Size of All the files including hidden files in the 'dir' directory

ls command will not list the actual size of directories(why?). Therefore, we use du for this purpose.

Checking Directory sizes

du -sh directory_name    #Gives you the summarized(-s) size of the directory in human readable(-h) format
du -bsh * #Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format

Including -h option in any of the above commands (for Ex: ls -lh * or du -sh) will give you size in human readable format (kb, mb,gb, ...)

For more information see man ls and man du

How do you get the file size in C#?

FileInfo.Length will return the length of file, in bytes (not size on disk), so this is what you are looking for, I think.



Related Topics



Leave a reply



Submit