In Focus

How to get a file size in C#

C# Get File Size. Learn how to get a file size in bytes using C# using the FileInfo.Length property.

C# Get File Size

 
The Length property of the FileInfo class returns the size of a file in bytes. The following code snippet returns the size of a file. Don't forget to import System.IO and System.Text namespaces in your project.
  1. // Get file size  
  2. long size = fi.Length;  
  3. Console.WriteLine("File Size in Bytes: {0}", size);  

C# Get File Size Code Example

 
Here is a complete code example. The following code example uses a FileInfo class to create an object by passing a complete filename. The FileInfo class provides properties to get information about a file such as file name, size, full path, extension, directory name, is read only, when the file was created and last updated.
 
Note: Make sure to replace the file name with your file name. Also, you can comment //Create a new file code if you already have a file. You may also want to convert the size from bytes to KB, MB, and GB by dividing bytes by 1024, 1024x1024 and so on. 
  1. // Full file name   
  2. string fileName = @"C:\Temp\MaheshTXFI.txt";  
  3. FileInfo fi = new FileInfo(fileName);  
  4.   
  5. // Create a new file   
  6. using (FileStream fs = fi.Create())  
  7. {  
  8.     Byte[] txt = new UTF8Encoding(true).GetBytes("New file.");  
  9.     fs.Write(txt, 0, txt.Length);  
  10.     Byte[] author = new UTF8Encoding(true).GetBytes("Author Mahesh Chand");  
  11.     fs.Write(author, 0, author.Length);  
  12. }  
  13.   
  14. // Get File Name  
  15. string justFileName = fi.Name;  
  16. Console.WriteLine("File Name: {0}", justFileName);  
  17. // Get file name with full path   
  18. string fullFileName = fi.FullName;  
  19. Console.WriteLine("File Name: {0}", fullFileName);  
  20. // Get file extension   
  21. string extn = fi.Extension;  
  22. Console.WriteLine("File Extension: {0}", extn);  
  23. // Get directory name   
  24. string directoryName = fi.DirectoryName;  
  25. Console.WriteLine("Directory Name: {0}", directoryName);  
  26. // File Exists ?  
  27. bool exists = fi.Exists;  
  28. Console.WriteLine("File Exists: {0}", exists);  
  29. if (fi.Exists)  
  30. {  
  31.     // Get file size  
  32.     long size = fi.Length;  
  33.     Console.WriteLine("File Size in Bytes: {0}", size);  
  34.     // File ReadOnly ?  
  35.     bool IsReadOnly = fi.IsReadOnly;  
  36.     Console.WriteLine("Is ReadOnly: {0}", IsReadOnly);  
  37.     // Creation, last access, and last write time   
  38.     DateTime creationTime = fi.CreationTime;  
  39.     Console.WriteLine("Creation time: {0}", creationTime);  
  40.     DateTime accessTime = fi.LastAccessTime;  
  41.     Console.WriteLine("Last access time: {0}", accessTime);  
  42.     DateTime updatedTime = fi.LastWriteTime;  
  43.     Console.WriteLine("Last write time: {0}", updatedTime);  
  44. }       
Download complete free book here: FileInfo in C#
 
You may want to convert the file size from bytes to KB, MB, or GB. Here is code example: Convert Bytes To KB, MB, GB in C#