Private bool download (string DownLoadURL, string savepath)
{
Stream responsestream = NULL;
Filestream = NULL;
Streamreader reader = NULL;
Try
{
Ftpwebrequest downloadrequest = (ftpwebrequest) webrequest. Create (DownLoadURL );
Downloadrequest. Credentials = new networkcredential (txtuser. Text. Trim (), txtpwd. Text. Trim ());
Ftpwebresponse downloadresponse = (ftpwebresponse) downloadrequest. getresponse ();
Responsestream = downloadresponse. getresponsestream ();
String filename = path. getfilename (downloadrequest. requesturi. absolutepath );
If (filename. Length = 0)
{
Reader = new streamreader (responsestream );
Console. writeline (reader. readtoend ());
}
Else
{
Filestream = file. Create (savepath + filename );
Byte [] buffer = new byte [1024];
Int bytesread;
While (true)
{
Bytesread = responsestream. Read (buffer, 0, buffer. Length );
If (bytesread = 0)
Break;
Filestream. Write (buffer, 0, bytesread );
}
}
Return true;
}
Catch (uriformatexception ex)
{
MessageBox. Show (ex. Message );
Return false;
}
Catch (webexception ex)
{
MessageBox. Show (ex. Message );
Return false;
}
Catch (ioexception ex)
{
MessageBox. Show (ex. Message );
Return false;
}
Finally
{
If (reader! = NULL)
Reader. Close ();
Else if (responsestream! = NULL)
Responsestream. Close ();
If (filestream! = NULL)
Filestream. Close ();
}
}