I'm sorry Jon. I don't see an attachment. Could you send
it again.
Thank You,
[color=blue]
>-----Original Message-----
>FTP is not implemented in the .NET framework.
>
>See attachment.
>
>HTH,
>Jon Davis
>
>
>"Tony" <traimo@cosmi.c om> wrote in message
>news:054001c36 382$66cf5d30$a3 01280a@phx.gbl. ..[color=green]
>> How do you put/get a file via ftp from a C# Windows App?
>> I can't seem to find any classes that can do this.
>>
>> Tahnk You,[/color]
>
>
>[/color]
"Tony" <traimo@cosmi.c om> wrote in message
news:048c01c363 85$0460aa70$a40 1280a@phx.gbl.. .[color=blue]
> I'm sorry Jon. I don't see an attachment. Could you send
> it again.
>
> Thank You,
>[/color]
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sock ets;
using System.Diagnost ics;
using System.Runtime. Remoting;
using System.Runtime. Remoting.Messag ing;
/*
* FTP Client library in C#
* Author: Jaimon Mathew
* mailto:jaimonma thew@rediffmail .com
* http://www.csharphelp.com/archives/archive9.html
*
* Adapted for use by Dan Glass 07/03/03
*
* Modified / adapted again by Jon Davis 08/15/2003
* added DirExists(), GetCurrentDir()
*
*/
public class FtpException : Exception
{
public FtpException(st ring message) : base(message) {
_resultcode = -1;
}
public FtpException(st ring message, int resultCode) : base(message) {
_resultcode = resultCode;
}
public FtpException(st ring message, Exception innerException) :
base(message,in nerException) {
_resultcode = -1;
}
public FtpException(st ring message, int resultCode, Exception
innerException) : base(message,in nerException) {
_resultcode = resultCode;
}
private int _resultcode;
public int ResultCode {
get {
return _resultcode;
}
}
}
/// <summary>
/// Display all communications to the debug log
/// </summary>
public bool VerboseDebuggin g
{
get
{
return this.verboseDeb ugging;
}
set
{
this.verboseDeb ugging = value;
}
}
/// <summary>
/// Remote server port. Typically TCP 21
/// </summary>
public int Port
{
get
{
return this.port;
}
set
{
this.port = value;
}
}
/// <summary>
/// Timeout waiting for a response from server, in seconds.
/// </summary>
public int Timeout
{
get
{
return this.timeoutSec onds;
}
set
{
this.timeoutSec onds = value;
}
}
/// <summary>
/// Gets and Sets the name of the FTP server.
/// </summary>
/// <returns></returns>
public string Server
{
get
{
return this.server;
}
set
{
this.server = value;
}
}
/// <summary>
/// Gets and Sets the port number.
/// </summary>
/// <returns></returns>
public int RemotePort
{
get
{
return this.port;
}
set
{
this.port = value;
}
}
/// <summary>
/// GetS and Sets the remote directory.
/// </summary>
public string RemotePath
{
get
{
return this.remotePath ;
}
set
{
this.remotePath = value;
}
}
/// <summary>
/// Gets and Sets the username.
/// </summary>
public string Username
{
get
{
return this.username;
}
set
{
this.username = value;
}
}
/// <summary>
/// Gets and Set the password.
/// </summary>
public string Password
{
get
{
return this.password;
}
set
{
this.password = value;
}
}
/// <summary>
/// If the value of mode is true, set binary mode for downloads, else,
Ascii mode.
/// </summary>
public bool BinaryMode
{
get
{
return this.binMode;
}
set
{
if ( this.binMode == value ) return;
if ( value )
sendCommand("TY PE I");
else
sendCommand("TY PE A");
if ( this.resultCode != 200 ) throw new
FtpException(re sult.Substring( 4), this.resultCode );
}
}
/// <summary>
/// Login to the Remote server.
/// </summary>
public void Login()
{
if ( this.loggedin ) this.Close();
Debug.WriteLine ("Opening connection to " + this.server, "FtpClient" );
IPAddress addr = null;
IPEndPoint ep = null;
try
{
this.clientSock et = new Socket( AddressFamily.I nterNetwork,
SocketType.Stre am, ProtocolType.Tc p );
addr = Dns.Resolve(thi s.server).Addre ssList[0];
ep = new IPEndPoint( addr, this.port );
this.clientSock et.Connect(ep);
}
catch(Exception ex)
{
// doubtfull
if ( this.clientSock et != null && this.clientSock et.Connected )
this.clientSock et.Close();
throw new FtpException("C ouldn't connect to remote server",ex);
}
if ( this.message.In dexOf( "No such file or directory" ) != -1 )
msg = new string[]{};
this.readRespon se();
if ( this.resultCode != 226 )
msg = new string[]{};
// throw new FtpException(re sult.Substring( 4));
return msg;
}
/// <summary>
/// Return the size of a file.
/// </summary>
/// <param name="fileName" ></param>
/// <returns></returns>
public long GetFileSize(str ing fileName)
{
if ( !this.loggedin ) this.Login();
this.sendComman d("SIZE " + fileName);
long size=0;
else
throw new FtpException(th is.result.Subst ring(4), this.resultCode );
return size;
}
/// <summary>
/// Download a file to the Assembly's local directory,
/// keeping the same file name.
/// </summary>
/// <param name="remFileNa me"></param>
public void Download(string remFileName)
{
this.Download(r emFileName,"",f alse);
}
/// <summary>
/// Download a remote file to the Assembly's local directory,
/// keeping the same file name, and set the resume flag.
/// </summary>
/// <param name="remFileNa me"></param>
/// <param name="resume"></param>
public void Download(string remFileName,Boo lean resume)
{
this.Download(r emFileName,"",r esume);
}
/// <summary>
/// Download a remote file to a local file name which can include
/// a path. The local file name will be created or overwritten,
/// but the path must exist.
/// </summary>
/// <param name="remFileNa me"></param>
/// <param name="locFileNa me"></param>
public void Download(string remFileName,str ing locFileName)
{
this.Download(r emFileName,locF ileName,false);
}
/// <summary>
/// Download a remote file to a local file name which can include
/// a path, and set the resume flag. The local file name will be
/// created or overwritten, but the path must exist.
/// </summary>
/// <param name="remFileNa me"></param>
/// <param name="locFileNa me"></param>
/// <param name="resume"></param>
public void Download(string remFileName,str ing locFileName,Boo lean resume)
{
if ( !this.loggedin ) this.Login();
this.BinaryMode = true;
Debug.WriteLine ("Downloadin g file " + remFileName + " from " + server +
"/" + RemotePath, "FtpClient" );
if (locFileName.Eq uals(""))
{
locFileName = remFileName;
}
FileStream output = null;
if ( !File.Exists(lo cFileName) )
output = File.Create(loc FileName);
else
output = new FileStream(locF ileName,FileMod e.Open);
Socket cSocket = createDataSocke t();
long offset = 0;
if ( resume )
{
offset = output.Length;
if ( offset > 0 )
{
this.sendComman d( "REST " + offset );
if ( this.resultCode != 350 )
{
//Server dosnt support resuming
offset = 0;
Debug.WriteLine ("Resuming not supported:" + result.Substrin g(4),
"FtpClient" );
}
else
{
Debug.WriteLine ("Resuming at offset " + offset, "FtpClient" );
output.Seek( offset, SeekOrigin.Begi n );
}
}
}
this.sendComman d("RETR " + remFileName);
if ( this.resultCode != 150 && this.resultCode != 125 )
{
throw new FtpException(th is.result.Subst ring(4), this.resultCode );
}
/// <summary>
/// Delete a file from the remote FTP server.
/// </summary>
/// <param name="fileName" ></param>
public void DeleteFile(stri ng fileName)
{
if ( !this.loggedin ) this.Login();
this.sendComman d( "DELE " + fileName );
if ( this.resultCode != 250 ) throw new
FtpException(th is.result.Subst ring(4), this.resultCode );
/// <summary>
/// Rename a file on the remote FTP server.
/// </summary>
/// <param name="oldFileNa me"></param>
/// <param name="newFileNa me"></param>
/// <param name="overwrite ">setting to false will throw exception if it
exists</param>
public void RenameFile(stri ng oldFileName,str ing newFileName, bool
overwrite)
{
if ( !this.loggedin ) this.Login();
this.sendComman d( "RNFR " + oldFileName );
if ( this.resultCode != 350 ) throw new
FtpException(th is.result.Subst ring(4), this.resultCode );
if ( !overwrite && this.GetFileLis t(newFileName). Length > 0 ) throw new
FtpException("F ile already exists");
this.sendComman d( "RNTO " + newFileName );
if ( this.resultCode != 250 ) throw new
FtpException(th is.result.Subst ring(4), this.resultCode );
/// <summary>
/// Delete a directory on the remote FTP server.
/// </summary>
/// <param name="dirName"> </param>
public void RemoveDir(strin g dirName)
{
if ( !this.loggedin ) this.Login();
this.sendComman d( "RMD " + dirName );
if ( this.resultCode != 250 ) throw new
FtpException(th is.result.Subst ring(4), this.resultCode );
/// <summary>
/// when doing data transfers, we need to open another socket for it.
/// </summary>
/// <returns>Connec ted socket</returns>
private Socket createDataSocke t()
{
this.sendComman d("PASV");
if ( this.resultCode != 227 ) throw new
FtpException(th is.result.Subst ring(4), this.resultCode );
int index1 = this.result.Ind exOf('(');
int index2 = this.result.Ind exOf(')');
Comment