Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do you get the file size in C#?
Getting the file size in C# can be accomplished using several methods. The most common approach is using the FileInfo class, which provides the Length property to retrieve the size of a file in bytes. You can also use static methods from the File class for quick operations without creating instances.
The FileInfo class is part of the System.IO namespace and provides comprehensive file manipulation capabilities including size retrieval, creation dates, and access permissions.
Syntax
Using FileInfo class to get file size −
FileInfo fileInfo = new FileInfo(filePath); long fileSize = fileInfo.Length;
Using File class static method −
long fileSize = new FileInfo(filePath).Length;
Using FileInfo.Length Property
The Length property of the FileInfo class returns the file size in bytes. This is the most straightforward method −
using System;
using System.IO;
class Program {
public static void Main() {
string filePath = "sample.txt";
// Create a sample file for demonstration
File.WriteAllText(filePath, "Hello, World! This is a test file.");
FileInfo fileInfo = new FileInfo(filePath);
if (fileInfo.Exists) {
Console.WriteLine("File: " + fileInfo.Name);
Console.WriteLine("Size: " + fileInfo.Length + " bytes");
} else {
Console.WriteLine("File does not exist.");
}
}
}
The output of the above code is −
File: sample.txt Size: 34 bytes
Getting File Sizes for Multiple Files
You can iterate through multiple files in a directory and get their sizes using DirectoryInfo and GetFiles() method −
using System;
using System.IO;
class Program {
public static void Main() {
// Create sample files for demonstration
File.WriteAllText("file1.txt", "This is file one.");
File.WriteAllText("file2.txt", "This is file two with more content.");
File.WriteAllText("file3.txt", "File three.");
string currentDirectory = Directory.GetCurrentDirectory();
DirectoryInfo di = new DirectoryInfo(currentDirectory);
FileInfo[] files = di.GetFiles("*.txt");
Console.WriteLine("Files in current directory:");
foreach (FileInfo file in files) {
Console.WriteLine("The size of {0} is {1} bytes.", file.Name, file.Length);
}
}
}
The output of the above code is −
Files in current directory: The size of file1.txt is 17 bytes. The size of file2.txt is 33 bytes. The size of file3.txt is 11 bytes.
Using One-Line File Size Retrieval
For quick file size retrieval without creating a FileInfo instance, you can use this concise approach −
using System;
using System.IO;
class Program {
public static void Main() {
string fileName = "data.txt";
// Create sample file
File.WriteAllText(fileName, "Sample data for size calculation.");
// One-line file size retrieval
long fileSize = new FileInfo(fileName).Length;
Console.WriteLine("File size: " + fileSize + " bytes");
Console.WriteLine("File size: " + (fileSize / 1024.0) + " KB");
}
}
The output of the above code is −
File size: 30 bytes File size: 0.029296875 KB
Common Use Cases
| Method | Use Case | Performance |
|---|---|---|
| FileInfo.Length | When you need multiple file properties | Good for multiple operations |
| new FileInfo(path).Length | Quick one-time size check | Best for single operation |
| DirectoryInfo.GetFiles() | Processing multiple files | Efficient for batch operations |
Conclusion
Getting file size in C# is straightforward using the FileInfo.Length property, which returns the size in bytes. Use FileInfo instances when you need multiple file properties, or the one-line approach for quick size retrieval. Always check if the file exists before accessing its properties to avoid exceptions.
