Home > OS >  Set the save path winform download files from the server
Set the save path winform download files from the server

Time:10-09

My code is as follows:
Public void DownloadFile (URL string, the string filename, System. Windows, Forms the ProgressBar prog)
{
System.Net.HttpWebRequest Myrq=(System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create (URL);
System.Net.HttpWebResponse myrp=(System.Net.HttpWebResponse) Myrq. The method GetResponse ();
Long totalBytes=myrp. ContentLength;//get request return the content length
If (prog!=null)
{
Prog. Maximum=(int) totalBytes;//returns the length of the assignment to the progress bar control
}
System. IO. Stream st=myrp. GetResponseStream ();
System. IO. Stream so=new System. IO. FileStream (filename, System. IO. FileMode. Create);
Long totalDownloadedByte=0;
Byte [] by=new byte [1024].
Int osize=st. Read (by 0, (int) by. Length);
While (osize & gt; 0)
{
TotalDownloadedByte=osize + totalDownloadedByte;
System. Windows. Forms. Application. DoEvents ();
So, the Write (by 0, osize);
If (prog!=null)
{
Prog. Value=https://bbs.csdn.net/topics/totalDownloadedByte (int);
}
Osize=st. Read (by 0, (int) by. Length);
}
So, Close ();
St. Close ();
Try
{

}
The catch (System. Exception)
{
Throw;
}
}
This method is the path of the download is fixed, how change can save to other position? Or it is better to let the user choose their own,
  • Related