Click here to Skip to main content
Licence CPOL
First Posted 18 Apr 2009
Views 14,806
Downloads 211
Bookmarked 22 times

Get Size of a File from the Internet

By | 18 Apr 2009 | Article
C# program to get the size of a file from the internet

Introduction

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. 

Background

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);
}
else              
{                
    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";
        }
        else
        {
            result = ContentLength / 1024;
            kbmbgb.Text = "KB";                        
        }
        File_Size = result.ToString("0.00");
        sizevaluelabel.Text = File_Size;
    }
} 

The code behind clear URL button is: 

textBox1.Clear(); 

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. 

History

  • 18th April, 2009: Initial post

License

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

About the Author

Vipin.150



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIf content-length = -1 try this Pinmemberanushripatil1:06 8 Dec '11  
GeneralExcellent! [modified] Pinmemberprojectzombie21:48 30 Oct '11  
GeneralDownloading file PinmemberBrendan Chong23:00 24 Apr '09  
GeneralRe: Downloading file PinmemberVipin.1504:44 27 Apr '09  
GeneralUI suggestions PinmemberBrendan Chong22:50 24 Apr '09  
GeneralRe: UI suggestions PinmemberVipin.1502:23 27 Apr '09  
GeneralTimely article PinmemberLloyd (Chris) Wilson4:15 21 Apr '09  
GeneralMy vote of 2 PinmemberEugene Sichkar10:06 19 Apr '09  
GeneralRe: My vote of 2 PinmemberVipin.1502:04 20 Apr '09  
GeneralRe: My vote of 2 PinmemberDutchMafia11:52 25 Sep '09  
GeneralInteresting article PinmemberSteven Relis12:48 18 Apr '09  
GeneralRe: Interesting article PinmemberVipin.15015:55 18 Apr '09  
GeneralHi there PinmemberHofstraProgrammer2:00 18 Apr '09  
GeneralRe: Hi there PinmemberVipin.1503:39 18 Apr '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 18 Apr 2009
Article Copyright 2009 by Vipin.150
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid