Showing posts with label Windows Forms. Show all posts
Showing posts with label Windows Forms. Show all posts
Wednesday, February 27, 2013
How to download file using C#?
In some of the cases we need to download file from the Internet, In this article I am explaining-
How to download file using C#?
For this I have used WebResponse, WebRequest Classes so we need to include System.Net Namespace.To download the file from Internet we have to make request to particular object so that I have used webRequest class object. After getting the response from the server we need to handle that response so I have used webResponse class object.
For displaying downloading process I have used ProgressBar control.
Here is the code-
private void btnDownLoadFile_Click(object sender, EventArgs e)
{
WebRequest requestObj ;
WebResponse responseObj;
Stream readerData ;
Stream writerData ;
byte[] data = new byte[1024];
int count = 0;
int total = 0;
this.Text = "Downloading file .....";
string DestinationFileName= "TargetLocation";
string SourceLocation= "SourceLocation";
Application.DoEvents();
try
{
requestObj = WebRequest.Create(SourceLocation);
responseObj = requestObj.GetResponse();
readerData = responseObj.GetResponseStream();
progressBar1.Maximum = Convert.ToInt32(responseObj .ContentLength);
progressBar1.Value = 0;
total = 0;
writerData = File.Create(DestinationFileName);
while (true)
{
count = readerData .Read(data, 0, 1024);
if (count <= 0)
{
break;
}
writerData.Write(data, 0, count);
total += 1024;
if (total < progressBar1.Maximum)
progressBar1.Value = total;
Application.DoEvents();
}
readerData .Close();
writerData.Close();
MessageBox.Show("Download completed...");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
In above code, I am making request to download file using WebRequest Class object, for that I am passing source file url to download file.
requestObj = WebRequest.Create(TargetLocation);
After making request we will get response, using this line of code-
responseObj = requestObj.GetResponse();
Now using using Stream class object I am reading and writing data to the target file.
count = readerData .Read(data, 0, 1024);
writerData.Write(data, 0, count);
Meanwhile for displaying the download process, I am showing progressbar. Based on data read and write progressbar value will be increased which will show downloading process.
Output-
Thanks
Thursday, January 3, 2013
How to make Single Click and Double Click Events work together in windows form?
While working with Windows Application, This is common problem that- Single Click and Double Click Events don't work together (If you have any processing code in Single Click Event like- Showing message using MessageBox), means if we have written some code in Single Click Event and some lines of code in Double Click Event then always Single Click Event code will be executed.
I also faced same problem and came with one workaround.
See this example-
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("Single Click Event");
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show("Double Click Event");
}
In above lines of code always Single Click Event code will be executed and always output will be - Single Click only.
In this article, I am explaining - How to make Single Click and Double Click Events work together?
Workaround-
To resolve this problem, I have taken one Timer Control, using Timer control I am handling the Single and Double Click Events functionalities.
Here is the code-
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = SystemInformation.DoubleClickTime;
}
private void Form1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
timer1.Enabled = false;
MessageBox.Show("Double Click Event");
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
MessageBox.Show("Single Click Event");
}
In above code, first I am setting Timer Interval as System Double Click Time value. This can be different based on Operating System Settings.
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = SystemInformation.DoubleClickTime;
}
Now in Single Click Event, I am just enabling the Timer to execute the code which is written in Tick() Event of Timer.
private void Form1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
In Tick() event write the code which you want to execute in Single Click Event.
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
MessageBox.Show("Single Click Event");
}
Here I am disabling the Timer Control also because this code will be executed once.
Now this is Double Click Event code-
private void Form1_DoubleClick(object sender, EventArgs e)
{
timer1.Enabled = false;
MessageBox.Show("Double Click Event");
}
Here I am just disabling the Timer Control; here you need to put actual Double Click Event code.
Here all the functionalities depend on Timer Control, Here making Timer control enable and disable is making trick for us.
Try this code.
Thanks
Subscribe to:
Posts (Atom)
