How do you get the file size in C#?

2019-01-05 02:15发布

I need a way to get the size of a file using C#, and not the size on disk. How is this possible?

Currently I have this loop

foreach (FileInfo file in downloadedMessageInfo.GetFiles())
{
    //file.Length (will this work)
}

Will this return the size or the size on disk?

标签: c# filesize
7条回答
萌系小妹纸
2楼-- · 2019-01-05 02:51

MSDN FileInfo.Length says that it is "the size of the current file in bytes."

My typical Google search for something like this is: msdn FileInfo

查看更多
爷、活的狠高调
3楼-- · 2019-01-05 02:52

Size on disk might be different, if you move the file to another filesystem (FAT16, NTFS, EXT3, etc)

As other answerers have said, this will give you the size in bytes, not the size on disk.

查看更多
来,给爷笑一个
4楼-- · 2019-01-05 02:54

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

查看更多
淡お忘
5楼-- · 2019-01-05 02:54

FileInfo.Length will do the trick (per MSDN it "[g]ets the size, in bytes, of the current file.") There is a nice page on MSDN on common I/O tasks.

查看更多
爷、活的狠高调
6楼-- · 2019-01-05 02:56

It returns the file contents length

查看更多
\"骚年 ilove
7楼-- · 2019-01-05 03:11

i get the size of file with File.ReadAllBytes(@"D:\\testPA.txt").Length

查看更多
登录 后发表回答