I have a made a small tool which gets file size and name of the URL but it takes time (approx 4 seconds) when I run the code and enter a file URL it takes time for the user it will look like it is not working.
I want to show a progress bar until the data is received so that user may not think that the application is not working.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Multi_Tool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
{
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));
FileType.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;
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
}
}