简体   繁体   中英

How to get correct pdf size

Hi I'm using some code as follows:

var f = new FileInfo(@"C:\sample.pdf");
var size = f.Length

When I inspect the file in explorer it shows a size of 214KB.

My code shows a size of 218.882KB.

Is this difference due to explorer not including metadata?

Can somebody tell me how through code I could get the 214KB figure?

尝试

var fileLengthInKB = f.Length / 1024.0;

Your C# code shows integer number.
Try calculating using those rules:

1 KB = 1024 B
1 MB = 1024 KB
etc.

Should match to what explorer shows.

Also, in the explorer compare 'Size' vs 'Size on Disk'.

I already up-voted the right answer, but wanted to note that Windows is "incorrectly" reporting kibibytes (KiB), not kilobytes (KB).

While the KB was traditionally calculated as 2^10 bytes (1024) for decades, the IEC redefined the KB in SI terms (10^3 bytes) in 1999 and created the newer KiB unit of measure to take its place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM