Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the file size in C#?

Tags:

c#

filesize

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?

like image 789
JL. Avatar asked Sep 04 '09 18:09

JL.


People also ask

How do you find the file size?

Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

Which of the following functions are used to determine size of a file in C?

ftell() in C This function is used to get the total size of file after moving the file pointer at the end of the file. It returns the current position in long type and file can have more than 32767 bytes of data.

How do I check the size of a file in MB?

format("The size of the file: %d bytes", fileSize); These methods will output the size in Bytes. So to get the MB size, you need to divide the file size from (1024*1024).

How do I check the size of a file in Linux?

The best Linux command to check file size is using du command. What we need is to open the terminal and type du -sh file name in the prompt. The file size will be listed on the first column. The size will be displayed in Human Readable Format.


1 Answers

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

long length = new System.IO.FileInfo(path).Length; 
like image 103
live-love Avatar answered Sep 18 '22 04:09

live-love