find the size of a file in csharp

To find the size of a file in C#, you can use the FileInfo class from the System.IO namespace. It provides a Length property which returns the size of the file in bytes. Here's an example:

main.cs
using System.IO;

string filePath = "C:\\example\\file.txt";
FileInfo fileInfo = new FileInfo(filePath);
long fileSizeInBytes = fileInfo.Length;
Console.WriteLine($"Size of {filePath} is {fileSizeInBytes} bytes.");
215 chars
7 lines

In this example, we first create an instance of the FileInfo class with the file path. Then we access the Length property to get the size of the file in bytes. Finally, we display the results using Console.WriteLine().

gistlibby LogSnag