Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#

Get Size of a File from the Internet

Rate me:
Please Sign up or sign in to vote.
4.07/5 (8 votes)
18 Apr 2009CPOL1 min read 54.8K   1.1K   28   15
C# program to get the size of a file from the internet
Image 1

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:

C#
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: 

C#
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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVote of 4 Pin
qualityking4-Dec-14 19:43
qualityking4-Dec-14 19:43 
QuestionSimple change to get exact size! Pin
Muhammad Shuaib14-Jun-12 6:17
Muhammad Shuaib14-Jun-12 6:17 
QuestionIf content-length = -1 try this Pin
anushripatil8-Dec-11 1:06
anushripatil8-Dec-11 1:06 
Thanks for code.
Also,I wasnt getting content length for my js file. I searched on net & found this article
http://bytes.com/topic/c-sharp/answers/625274-webresponse-contentlength-1-a

System.Net.WebRequest req = System.Net.HttpWebRequest.Create(url);
req.Headers.Add("Accept-Encoding: gzip,deflate");

This worked for me . Smile | :)
GeneralExcellent! Pin
projectzombie30-Oct-11 21:48
projectzombie30-Oct-11 21:48 
GeneralDownloading file Pin
Brendan Chong24-Apr-09 23:00
Brendan Chong24-Apr-09 23:00 
GeneralRe: Downloading file Pin
Vipin.15027-Apr-09 4:44
Vipin.15027-Apr-09 4:44 
GeneralUI suggestions Pin
Brendan Chong24-Apr-09 22:50
Brendan Chong24-Apr-09 22:50 
GeneralRe: UI suggestions Pin
Vipin.15027-Apr-09 2:23
Vipin.15027-Apr-09 2:23 
GeneralTimely article Pin
Lloyd (Chris) Wilson21-Apr-09 4:15
Lloyd (Chris) Wilson21-Apr-09 4:15 
GeneralMy vote of 2 Pin
Eugene Sichkar19-Apr-09 10:06
Eugene Sichkar19-Apr-09 10:06 
GeneralRe: My vote of 2 Pin
Vipin.15020-Apr-09 2:04
Vipin.15020-Apr-09 2:04 
GeneralInteresting article Pin
Steven Relis18-Apr-09 12:48
Steven Relis18-Apr-09 12:48 
GeneralRe: Interesting article Pin
Vipin.15018-Apr-09 15:55
Vipin.15018-Apr-09 15:55 
GeneralHi there Pin
HofstraProgrammer18-Apr-09 2:00
HofstraProgrammer18-Apr-09 2:00 
GeneralRe: Hi there Pin
Vipin.15018-Apr-09 3:39
Vipin.15018-Apr-09 3: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.