How do you get the file size in C#?

核能气质少年 提交于 2019-11-26 06:35:12

问题


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?


回答1:


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




回答2:


If you have already a file path as input, this is the code you need:

long length = new System.IO.FileInfo(path).Length;



回答3:


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.




回答4:


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




回答5:


It returns the file contents length




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/1380839/how-do-you-get-the-file-size-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!