Windows-8.1

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";
     }
 }

Vivek Maskara

I am pursuing BTech in Software Engineering from Delhi Technological University. I develop apps, create websites and blog about my app experiences.

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding
Close