Click here to Skip to main content
14,735,745 members
Articles » Languages » C# » General
Posted 18 Apr 2009


28 bookmarked

Get Size of a File from the Internet

Rate me:
Please Sign up or sign in to vote.
3.73/5 (7 votes)
18 Apr 2009CPOL
C# program to get the size of a file from the internet
Image 1


This article shows a C# program that implements the functions required to get the size of a file from the internet. It includes one form that contains an area to type the URL, a button to get the file size, 3 labels that show file size, name and type respectively. 


The idea behind this project is: you have seen many download managers that contain an option to get the size of the file you are going to download. This information provides the basic idea of what the size of the file is and helps us to prioritize our downloads.  

Using the Code

Using this code is pretty simple. I used Microsoft Visual C# Express Edition for this project. 

The main form contains a text box where we have to type the URL. A button is used to clear the URL. The code behind Get File Size button is given below:

if (textBox1.Text == "")
    MessageBox.Show("You have not typed the URL", "URL Error", 
			MessageBoxButtons.OK, MessageBoxIcon.Error);
    string URL = textBox1.Text;
    string filetype = URL.Substring(URL.LastIndexOf(".") + 1, 
			(URL.Length - URL.LastIndexOf(".") - 1));
    filetypevalue.Text = filetype.ToUpper();
    string filename = URL.Substring(URL.LastIndexOf("/") + 1, 
			(URL.Length - URL.LastIndexOf("/") - 1));
    namelabel.Text = filename;
     System.Net.WebRequest req = System.Net.HttpWebRequest.Create(textBox1.Text);
    req.Method = "HEAD";
    System.Net.WebResponse resp = req.GetResponse();
    long ContentLength = 0;
    long result;
    if (long.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
        string File_Size;                   
        if (ContentLength >= 1073741824)
            result = ContentLength / 1073741824;
            kbmbgb.Text = "GB";
        else if (ContentLength >= 1048576)
            result = ContentLength / 1048576;
            kbmbgb.Text = "MB";
            result = ContentLength / 1024;
            kbmbgb.Text = "KB";                        
        File_Size = result.ToString("0.00");
        sizevaluelabel.Text = File_Size;

The code behind clear URL button is: 


Future Enhancements 

This program is in its preliminary stage. You can add or remove features according to your taste.

Some of the features to add are:

  1. Handle all exceptions (Program now handles exceptions when users click the get file size button without typing the URL.
  2. Recognize the extension and display an image. For example, if the extension is .mp3, an image of an audio file is to be displayed. 


  • 18th April, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
No Biography provided

Comments and Discussions

QuestionVote of 4 Pin
qualityking4-Dec-14 20:43
Memberqualityking4-Dec-14 20:43 
QuestionSimple change to get exact size! Pin
Muhammad Shuaib14-Jun-12 7:17
MemberMuhammad Shuaib14-Jun-12 7:17 
QuestionIf content-length = -1 try this Pin
anushripatil8-Dec-11 2:06
Memberanushripatil8-Dec-11 2:06 
GeneralExcellent! Pin
projectzombie30-Oct-11 22:48
Memberprojectzombie30-Oct-11 22:48 
GeneralDownloading file Pin
Brendan Chong25-Apr-09 0:00
MemberBrendan Chong25-Apr-09 0:00 
GeneralRe: Downloading file Pin
Vipin.15027-Apr-09 5:44
MemberVipin.15027-Apr-09 5:44 
GeneralUI suggestions Pin
Brendan Chong24-Apr-09 23:50
MemberBrendan Chong24-Apr-09 23:50 
GeneralRe: UI suggestions Pin
Vipin.15027-Apr-09 3:23
MemberVipin.15027-Apr-09 3:23 
GeneralTimely article Pin
Lloyd (Chris) Wilson21-Apr-09 5:15
MemberLloyd (Chris) Wilson21-Apr-09 5:15 
GeneralMy vote of 2 Pin
Eugene Sichkar19-Apr-09 11:06
MemberEugene Sichkar19-Apr-09 11:06 
GeneralRe: My vote of 2 Pin
Vipin.15020-Apr-09 3:04
MemberVipin.15020-Apr-09 3:04 
GeneralInteresting article Pin
Steven Relis18-Apr-09 13:48
MemberSteven Relis18-Apr-09 13:48 
GeneralRe: Interesting article Pin
Vipin.15018-Apr-09 16:55
MemberVipin.15018-Apr-09 16:55 
GeneralHi there Pin
HofstraProgrammer18-Apr-09 3:00
MemberHofstraProgrammer18-Apr-09 3:00 
GeneralRe: Hi there Pin
Vipin.15018-Apr-09 4:39
MemberVipin.15018-Apr-09 4:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.