Pipz

Hello,

I want to know how to use the System.Net.WebClient in my application. Here is the part of the code:

System.Net.WebClient client = new System.Net.WebClient();

client.DownloadFile("http://updates.com/index.xml","c:\\update.xml");

That would download the file "index.xml" and save it in the local computer in drive C under a new name "update.xml". I would like my application to determine the file size of the file to be downloaded. I would like to make a progress bar showing the percent downloaded in the file in relation to its file size. Then after, when the download finishes, a MessageBox will appear stating that the download is completed.

Even if the progress bar is not included, that would suffice. What I would like to have is that when the file was downloaded in local machine of the user, a messagebox will appear stating that the downloading of updates is finish.

Hope you can help me in here.

Thanks and more power!

Ichi



Re: Windows Forms General How to Create a Download Update Form?

Friendly Dog

Use asychornized download and it gives you everything you need.

 

WebClient wc = new WebClient();

....

wc.DownloadFileCompleted+=new AsyncCompletedEventHandler(wc_DownloadFileCompleted);

wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);

wc.DownloadFileAsync(new Uri("http://updates.com/index.xml"), "c:\\index.html");

....

void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

{

long rec = e.BytesReceived; //received bytes so far

long percent = e.ProgressPercentage; //progress percentage

long total = e.TotalBytesToReceive; // total bytes

}

...

void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

{

MessageBox.Show("Done");

}






Re: Windows Forms General How to Create a Download Update Form?

Friendly Dog

Be aware that your ASP.Net user may don't have write access to C:\




Re: Windows Forms General How to Create a Download Update Form?

Ichi_Programmer

All the codes works fine except the function

void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{

long rec = e.BytesReceived; //received bytes so far

long percent = e.ProgressPercentage; //progress percentage

long total = e.TotalBytesToReceive; // total bytes

}

C# generated an error.

Error 1 The type or namespace name 'DownloadProgressChangedEventArgs' could not be found (are you missing a using directive or an assembly reference ) C:\Documents and Settings\Ichi Ban\Local Settings\Application Data\Temporary Projects\DownloadUpdate\Form1.cs 30 56 DownloadUpdate

What would be the possible cause Its quite unclear for me.

Thanks and more power!

Ichi






Re: Windows Forms General How to Create a Download Update Form?

Friendly Dog

Are you using .Net 1.0 or 1.1 I believe this is only introuduced since 2.0




Re: Windows Forms General How to Create a Download Update Form?

Ichi_Programmer

I have found that currently, there is 2 .net framework in my computer

I saw it in the control panel in the add, remove program.

There it is:
-Microsoft .NET Framework 1.1
-Microsoft .NET Framework 2.0

Is that a big problem

Ichi





Re: Windows Forms General How to Create a Download Update Form?

Friendly Dog

I have 1.1, 2.0 and 3.0 on my machine. .Net framework is designed to be work side-by-side among multiple versions. Are you using VS 2003 or VS 2005 By default VS 2003 uses 1.1, 2005 uses 2.0




Re: Windows Forms General How to Create a Download Update Form?

Ichi_Programmer

I have fixed it already. =D

I just don't understand but I just tried a couple of changes.

OLD:

wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);


NEW:

wc.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);

Same goes to the function below.

void wc_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
...
}


Yet, I do not know the explanation. But I'm trying to check as to why is it. =D

Do you have any idea about this





Re: Windows Forms General How to Create a Download Update Form?

Friendly Dog

oh, you can put a using System.Net on top of your code then you don't need this System.net stuff in your code anymore.