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?
Try
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.
精彩评论