T
Thom Little
The following will download every file in a remote directory ...
public void DownloadDirectory(string strAddress, string strUsername,
string strPassword )
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(c_strFtp +
strAddress );
req.Credentials = new NetworkCredential(strUsername, strPassword );
req.Method = WebRequestMethods.Ftp.ListDirectory ;
FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
Stream rs = resp.GetResponseStream( );
StreamReader sr = new StreamReader(rs, System.Text.Encoding.UTF8 );
string[] strDirectory = sr.ReadToEnd().Trim( '\n' ).Split( '\n' );
sr.Close( );
rs.Close( );
resp.Close( );
foreach (string strFilename in strDirectory )
this.DownloadFile( strFilename.Trim( '\r'), strAddress, strUsername,
strPassword );
}
private void DownloadFile( string strFilename, string strAddress, string
strUsername, string strPassword )
{
byte[] buf = new byte[2047] ;
int iWork ;
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create( c_strFtp +
strAddress + strFilename );
req.Credentials = new NetworkCredential(strUsername, strPassword );
req.Method = WebRequestMethods.Ftp.DownloadFile ;
FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
Stream rs = resp.GetResponseStream( );
FileStream fs = new FileStream(this.tbAddressClient.Text.ToString() +
strFilename, FileMode.Create );
while ( ( iWork = rs.Read( buf, 0, buf.Length ) ) > 0 )
fs.Write( buf, 0, iWork );
fs.Close( );
rs.Close( );
}
.... the disadvantage is that every file downloaded requires a separate
connection to be established.
Is there an example of establishing a connection, downloading multiple
files, closing connections?
public void DownloadDirectory(string strAddress, string strUsername,
string strPassword )
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(c_strFtp +
strAddress );
req.Credentials = new NetworkCredential(strUsername, strPassword );
req.Method = WebRequestMethods.Ftp.ListDirectory ;
FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
Stream rs = resp.GetResponseStream( );
StreamReader sr = new StreamReader(rs, System.Text.Encoding.UTF8 );
string[] strDirectory = sr.ReadToEnd().Trim( '\n' ).Split( '\n' );
sr.Close( );
rs.Close( );
resp.Close( );
foreach (string strFilename in strDirectory )
this.DownloadFile( strFilename.Trim( '\r'), strAddress, strUsername,
strPassword );
}
private void DownloadFile( string strFilename, string strAddress, string
strUsername, string strPassword )
{
byte[] buf = new byte[2047] ;
int iWork ;
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create( c_strFtp +
strAddress + strFilename );
req.Credentials = new NetworkCredential(strUsername, strPassword );
req.Method = WebRequestMethods.Ftp.DownloadFile ;
FtpWebResponse resp = (FtpWebResponse)req.GetResponse( );
Stream rs = resp.GetResponseStream( );
FileStream fs = new FileStream(this.tbAddressClient.Text.ToString() +
strFilename, FileMode.Create );
while ( ( iWork = rs.Read( buf, 0, buf.Length ) ) > 0 )
fs.Write( buf, 0, iWork );
fs.Close( );
rs.Close( );
}
.... the disadvantage is that every file downloaded requires a separate
connection to be established.
Is there an example of establishing a connection, downloading multiple
files, closing connections?