Retrieve File Size From Remote URL using C#

The system API class System.Net.HttpWebRequest allows you to retrieve the file information programmatically. The size of each file/page generally available in the header section of web response, which you can query to get it.
Header contains Content-Length property value which gives you the size of the file in bytes. This you can later calculate to convert in MB or GB.

Here’s the source code that can be used:

private static string GetFileSize(Uri uriPath)
 {
     var webRequest = HttpWebRequest.Create(uriPath);
     webRequest.Method = "HEAD";
  
     using (var webResponse = webRequest.GetResponse())
     {
        var fileSize = webResponse.Headers.Get("Content-Length");
        var fileSizeInMegaByte = Math.Round(Convert.ToDouble(fileSize) / 1024.0 / 1024.0, 2);
        return fileSizeInMegaByte + " MB";
     }
 }