C# .NET: Get File Size in .NET

1/28/2014 7:40:38 PM

Get the size of a file in bytes, kilobytes, megabytes and gigabytes.

string directoy = @"C:\Folder1\Folder2"; //without @ symbol, backslashes need to be doubled up c:\\something\\something\\
string filename = "MyMovieFile.mp4";
string fullFilePath = System.IO.Path.Combine(directoy, filename); //easy way to combine paths and not worry about backslashes and stuff

long bytes = 0;
double kilobytes = 0;
double megabytes = 0;
double gibabytes = 0;

System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullFilePath);
if (fileInfo.Exists)
{
	bytes = fileInfo.Length;
	kilobytes = (double)bytes / 1024;
	megabytes = kilobytes / 1024;
	gibabytes = megabytes / 1024;
}