///
/// Delete a file from the remote FTP server.
///
///
public void deleteRemoteFile(string fileName)
{
if(!logined)
{
login();
}
sendCommand("DELE "+fileName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
}
///
/// Rename a file on the remote FTP server.
///
///
///
public void renameRemoteFile(string oldFileName,string
newFileName)
{
if(!logined)
{
login();
}
sendCommand("RNFR "+oldFileName);
if(retValue != 350)
{
throw new IOException(reply.Substring(4));
}
// known problem
// rnto will not take care of existing file.
// i.e. It will overwrite if newFileName exist
sendCommand("RNTO "+newFileName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
}
///
/// Create a directory on the remote FTP server.
///
///
public void mkdir(string dirName)
{
if(!logined)
{
login();
}
sendCommand("MKD "+dirName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
}
///
/// Delete a directory on the remote FTP server.
///
///
public void rmdir(string dirName)
{
if(!logined)
{
login();
}
sendCommand("RMD "+dirName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
}
///
/// Change the current working directory on the remote FTP server.
///
///
public void chdir(string dirName)
{
if(dirName.Equals("."))
{
return;
}
if(!logined)
{
login();
}
sendCommand("CWD "+dirName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
this.remotePath = dirName;
Console.WriteLine("Current directory is "+remotePath);
}
///
/// Close the FTP connection.
///
public void close()
{
Socket s = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep = new
IPEndPoint(Dns.Resolve(ipAddress).AddressList[0], port);
try
{
s.Connect(ep);
}
catch(Exception)
{
throw new IOException("Can't connect to remote server");
}
///
/// Set the name of the FTP server to connect to.
///
/// Server name
public void setRemoteHost(string remoteHost)
{
this.remoteHost = remoteHost;
}
///
/// Return the name of the current FTP server.
///
/// Server name
public string getRemoteHost()
{
return remoteHost;
}
///
/// Set the port number to use for FTP.
///
/// Port number
public void setRemotePort(int remotePort)
{
this.remotePort = remotePort;
}
///
/// Return the current port number.
///
/// Current port number
public int getRemotePort()
{
return remotePort;
}
///
/// Set the remote directory path.
///
/// The remote directory path
public void setRemotePath(string remotePath)
{
this.remotePath = remotePath;
}
///
/// Return the current remote directory path.
///
/// The current remote directory path.
public string getRemotePath()
{
return remotePath;
}
///
/// Set the user name to use for logging into the remote server.
///
/// Username
public void setRemoteUser(string remoteUser)
{
this.remoteUser = remoteUser;
}
///
/// Set the password to user for logging into the remote server.
///
/// Password
public void setRemotePass(string remotePass)
{
this.remotePass = remotePass;
}
///
/// Return a string array containing the remote directory's file list.
///
///
///
public string[] getFileList(string mask)
{
///
/// Login to the remote server.
///
public void login()
{
clientSocket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep = new
IPEndPoint(Dns.Resolve(remoteHost).AddressList[0], remotePort);
try
{
clientSocket.Connect(ep);
}
catch(Exception)
{
throw new IOException("Couldn't connect to remote server");
}
logined = true;
Console.WriteLine("Connected to "+remoteHost);
chdir(remotePath);
}
///
/// If the value of mode is true, set binary mode for downloads.
/// Else, set Ascii mode.
///
///
public void setBinaryMode(Boolean mode)
{
if(mode)
{
sendCommand("TYPE I");
}
else
{
sendCommand("TYPE A");
}
if (retValue != 200)
{
throw new IOException(reply.Substring(4));
}
}
///
/// Download a file to the Assembly's local directory,
/// keeping the same file name.
///
///
public void download(string remFileName)
{
download(remFileName,"",false);
}
///
/// Download a remote file to the Assembly's local directory,
/// keeping the same file name, and set the resume flag.
///
///
///
public void download(string remFileName,Boolean resume)
{
download(remFileName,"",resume);
}
///
/// 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.
///
///
///
public void download(string remFileName,string locFileName)
{
download(remFileName,locFileName,false);
}
///
/// 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.
///
///
///
///
public void download(string remFileName,string
locFileName,Boolean resume)
{
if(!logined)
{
login();
}
setBinaryMode(true);
Console.WriteLine("Downloading file "+remFileName+" from "+remoteHost + "/"+remotePath);
if (locFileName.Equals(""))
{
locFileName = remFileName;
}
if(!File.Exists(locFileName))
{
Stream st = File.Create(locFileName);
st.Close();
}
FileStream output = new
FileStream(locFileName,FileMode.Open);
Socket cSocket = createDataSocket();
long offset = 0;
if(resume)
{
offset = output.Length;
if(offset > 0 )
{
sendCommand("REST "+offset);
if(retValue != 350)
{
//throw new IOException(reply.Substring(4));
//Some servers may not support resuming.
offset = 0;
}
}
if(offset > 0)
{
if(debug)
{
Console.WriteLine("seeking to " + offset);
}
long npos = output.Seek(offset,SeekOrigin.Begin);
Console.WriteLine("new pos="+npos);
}
}