NEXFTP Ftp Client
** Not a perfect — Have some bugs which if you want you are more than Special Thanks to Paul Lockwood
Disclaimer : Anyone can use the code , but I would appreciate if you tag my name with it . Requirements : .NET framework || Visual.NET Envirnoment
Developed on : .NET (Beta 2) Below is snap shot of the GUI :
using System;
[assembly: AssemblyKeyFile("c:/Euler/programs/nexftp/nexftp.snk")]
namespace FtpLibrary {
public class NEXFTP {
public static Button b1 = new Button();
static void Main() {
public NEXFTP() {
public static String pro_ser() {
public void pro_win(String par) {
public void server_pwdResponse(String par1) {
public static void form_load() {
frm1 = new Form();
Label l1 = new Labe
upload.Click += new System.EventHandler(TC.Remote_transfer_upload);
// Objects inherited from FtpRemoteControl and FtpLocalControl
ListView Rview = RC.addListView();
frm1.Name ="First Form";
}
private static void Button_Click(object sender , EventArgs e) {
}
public NEXFTP Dcomm = new NEXFTP();
public void Remote_transfer_upload(object sender , EventArgs e) {
foreach (ListViewItem item in FtpLocalControl.LocalView.SelectedItems) }
}
FL.getListView(" ");
}
}
/***********************************************************************************************/
/* Below class is used as intial login connection protocol */
public NEXFTP conn = new NEXFTP();
public String Connect_Protocol(String Server) {
IPAddress hostadd = Dns.Resolve(Server).AddressList[0]; s.Connect(EPhost);
if (s.Connected)
Console.WriteLine("My local IpAddress is :" + IPAddress.Parse(((IPEndPoint)s.LocalEndPoint).Address.ToString()) +
s.Receive(welcome, 0, s.Available, SocketFlags.None);
}
return str;
}
public struct commandReply
/* Below functions are access control commands */
byte[] uok = new byte[512]; }
public void sys_pwd() {
public void sys_mkd(String ndir) {
public void sys_put(String cfile , String sfile) {
byte[] tyok = new byte[512];
do } reply = this.readCommandReply();
public void sys_get(String cFileDest , String sFile) {
acsize="";
// Now read data from the dataSocket
Byte[] readBytes;
}
public void sys_cwd(String pwd) {
public void sys_list(string Current_pwd) {
if (TYPE.code == 150) Byte[] readBytes; this.readCommandReply();
private Socket Get_List_Socket () Console.WriteLine("hi " + pasvAddress + " " + pasvPort); // Open the Data socket
commandReply reply;
if (s.Poll(1000, SelectMode.SelectRead)==true ) do
// Check id there are more lines to receive
reply.code = Convert.ToInt32(serverMessage.Substring(0,3)); conn.pro_win(reply.message);
}
public class FtpRemoteControl {
public static ListView RemoteView = new ListView();
public ListView addListView() {
t1.Location = new Point (80 ,40) ;
t1.Width = 130; t2.Width = 130 ;
cb1.Click += new System.EventHandler(Cdir_OKClick);
RemoteView.DoubleClick += new System.EventHandler(RemoteView_DoubleClick);
RemoteView.Columns.Add("Name", -2, HorizontalAlignment.Center);
// Create two ImageList objects.
// Initialize the ImageList objects with bitmaps.
//Assign the ImageList objects to the ListView.
return RemoteView;
Console.WriteLine("I am in list command"); do { for(int i=0; i<=Response_Parsed.Length-1; i++) if(i==1) { if (i>1) { substr = substr.Remove(1, 54); }
//Assign the ImageList objects to the ListView. }
public void RemoteView_Chdir_Click(object sender, System.EventArgs e) }
}
FC.sys_list(pwd); }
String Curre
Console.WriteLine("hi"); }
private void Cdir_OKClick(object sender, System.EventArgs e)
String Command = t1.Text.Trim(); }
String Command = t2.Text.Trim(); }
public struct commandReply
private ListViewItem addItem(ListViewItem arList , String substr , int i , String attr) {
private ListViewItem addSubItem(ListViewItem arList , String substr ) {
private void Refresh() {
} }
public class FtpLocalControl {
public static NEXFTP main = new NEXFTP();
public FtpLocalControl() {
}
public ListView addListView() {
fchdir.Size = new Size(400,130);
t1.Location = new Point (80 ,40) ;
t1.Width = 130; t2.Width = 130 ; LocalView.Bounds = new Rectangle(new Point(10,70), new Size(260,265)); LocalView.DoubleClick += new System.EventHandler(LocalView_DoubleClick); Refresh(); /* foreach (string ad in AllFiles) LocalView.Items.Add(faddItem(new ListViewItem() , ad.Remove(0, temp.Length+1))); foreach (string ad in AllFd) LocalView.Items.Add(daddItem(new ListViewItem() , ad.Remove(0, temp.Length))); foreach (string str in drives) LocalView.Items.Add(daddItem(new ListViewItem() , str.Remove(1,2))); }
return LocalView;
public void getListView(string path) {
ImageList imageListFile = new ImageList(); string[] AllFd = Directory.GetDirectories(current_dir(path) ); Refresh();
foreach (string ad in AllFiles) LocalView.Items.Add(faddItem(new ListViewItem() , ad.Remove(0, temp.Length+1)));
foreach (string ad in AllFd) LocalView.Items.Add(daddItem(new ListViewItem() , ad.Remove(0, temp.Length+1))); } foreach (string str in drives) LocalView.Items.Add(daddItem(new ListViewItem() , str.Remove(1,2))); } imageListFile.Images.Add(Bitmap.FromFile("c:\openfold.bmp"));
//Assign the ImageList objects to the ListView.
}
public void getParentView() {
ImageList imageListFile = new ImageList(); Refresh(); foreach (string ad in AllFd) LocalView.Items.Add(daddItem(new ListViewItem() , ad.Remove(0, temp.Length))); } imageListFile.Images.Add(Bitmap.FromFile("C:\isfile.bmp"));
//Assign the ImageList objects to the ListView.
}
}
}
}
public void LocalView_Rename_Click(object sender, System.EventArgs e) frename.Size = new Size(400,130); }
private void Rna_OKClick(object sender, System.EventArgs e) {
public void LocalView_Delete_Click(object sender, System.EventArgs e) foreach (ListViewItem item in LocalView.SelectedItems) try{
Console.WriteLine(item.SubItems[2].ToString());}
}
}
private void Refresh() { LocalView.Items.Add(daddItem(new ListViewItem() , cdup));
if (path.Equals("C")) if (path.Equals("D")) if (path.Equals("A")) return set; private string get_parent(string path) { return parentpath; }
}
}
NEXFTP is a simple and robust FTP client I earlier developed in java . In recent weeks I started
to port it to C# . Allthough the code works fine with most of the remote servers I cannot assure
you that it would work with everywhere.
welcome to fix them.
Code :
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Drawing.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Reflection;
public static Button b2 = new Button();
public static Button upload = new Button();
public static Button download = new Button();
public static TextBox t1 = new TextBox();
public static TextBox t2 = new TextBox();
public static TextBox t3 = new TextBox();
public static TextBox t4 = new TextBox();
public static Form frm1 ;// = new Form();
public static Button LCHDIR = new Button();
public static Button LMKDIR = new Button();
public static Button LVIEW = new Button();
public static Button LEXECUTE = new Button();
public static Button LRENAME = new Button();
public static Button LDELETE = new Button();
public static Button LREFRESH = new Button();
public static Button SCHDIR = new Button();
public static Button SMKDIR = new Button();
public static Button SVIEW = new Button();
public static Button SEXECUTE = new Button();
public static Button SRENAME = new Button();
public static Button SDELETE = new Button();
public static Button SREFRESH = new Button();
public static TextBox SCR;
public static FtpRemoteControl RC = new FtpRemoteControl();
public static FtpLocalControl LC = new FtpLocalControl();
public static Ftptransfer TC = new Ftptransfer();
form_load();
}
}
String server = t1.Text;
return server;
}
public String pro_usr() {
String user = t3.Text;
return user;
}
public String pro_pass() {
String pass = t4.Text;
return pass;
}
Console.WriteLine("@-Pro-Win");
SCR.AppendText(par + "\r\n");
}
Console.WriteLine("@-Pwd-Response");
SCR.AppendText( par1 + "\r\n");
}
SCR = new TextBox();
frm1.Size = new Size(800,500);
SCR.Size = new Size(780,73);
SCR.Multiline = true;
SCR.ScrollBars = ScrollBars.Vertical;
SCR.WordWrap = true;
SCR.AcceptsReturn = true;
SCR.Text = "Welcome to NEXFTP" + "\r\n";
SCR.Location = new Point(10, 380);
frm1.Text = "NEXFTP Simple FTP client";
frm1.HelpButton = true;
frm1.MaximizeBox = false;
frm1.MinimizeBox = false;
frm1.FormBorderStyle = FormBorderStyle.FixedDialog;
b1.Location = new Point (680 , 30); b2.Location = new Point (680 , 200);
upload.Location = new Point(370 , 150) ; download.Location = new Point(370 , 210);
upload.FlatStyle = FlatStyle.Popup; download.FlatStyle = FlatStyle.Popup;
upload.Image = Image.FromFile("C:\up.gif"); upload.Width = 25;
download.Image = Image.FromFile("C:\down.gif"); download.Width = 25;
LCHDIR.Location = new Point(275 , 90); LCHDIR.Text = "CHDIR";
LMKDIR.Location = new Point(275 , 150); LMKDIR.Text = "MKDIR";
LVIEW.Location = new Point(275 , 150); LVIEW.Text = "VIEW";
LEXECUTE.Location = new Point(275 , 190); LEXECUTE.Text ="EXECUTE";
LRENAME.Location = new Point(275 , 230); LRENAME.Text = "RENAME";
LDELETE.Location = new Point(275 , 210); LDELETE.Text = "DELETE";
LREFRESH.Location = new Point(275 , 270); LREFRESH.Text = "REFRESH";
LCHDIR.FlatStyle = FlatStyle.Popup;
LMKDIR.FlatStyle = FlatStyle.Popup;
LVIEW.FlatStyle = FlatStyle.Popup;
LEXECUTE.FlatStyle = FlatStyle.Popup;
LRENAME.FlatStyle = FlatStyle.Popup;
LDELETE.FlatStyle = FlatStyle.Popup;
LREFRESH.FlatStyle = FlatStyle.Popup;
SCHDIR.Location = new Point(690 , 90); SCHDIR.Text = "CHDIR";
SMKDIR.Location = new Point(690 , 150); SMKDIR.Text = "MKDIR";
SVIEW.Location = new Point(690 , 150); SVIEW.Text = "VIEW";
SEXECUTE.Location = new Point(690 , 190); SEXECUTE.Text = "EXECUTE";
SRENAME.Location = new Point(690 , 230); SRENAME.Text = "RENAME";
SDELETE.Location = new Point(690 , 210); SDELETE.Text = "DELETE";
SREFRESH.Location = new Point(690 , 270); SREFRESH.Text = "REFRESH";
SCHDIR.FlatStyle = FlatStyle.Popup;
SMKDIR.FlatStyle = FlatStyle.Popup;
SVIEW.FlatStyle = FlatStyle.Popup;
SEXECUTE.FlatStyle = FlatStyle.Popup;
SRENAME.FlatStyle = FlatStyle.Popup;
SDELETE.FlatStyle = FlatStyle.Popup;
SREFRESH.FlatStyle = FlatStyle.Popup;
LCHDIR.BackColor = Color.LightGray; LMKDIR.BackColor = Color.LightGray; LVIEW.BackColor = Color.LightGray;
LEXECUTE.BackColor = Color.LightGray; LRENAME.BackColor =Color.LightGray; LDELETE.BackColor = Color.LightGray;
LREFRESH.BackColor = Color.LightGray; SCHDIR.BackColor =Color.LightGray; SMKDIR.BackColor = Color.LightGray;
SVIEW.BackColor = Color.LightGray; SEXECUTE.BackColor =Color.LightGray; SRENAME.BackColor = Color.LightGray;
SDELETE.BackColor = Color.LightGray; SREFRESH.BackColor = Color.LightGray;
l(); Label l2 = new Label(); Label l3 = new Label(); Label l4 = new Label();
l1.Location = new Point (0 ,30) ; l2.Location = new Point (250 , 30);l3.Location = new Point (350 ,30); l4.Location = new Point (500 ,30);
l1.Width = 80; l2.Width = 35 ; l3.Width = 70 ; l4.Width = 70;
t1.Location = new Point (82 ,30) ; t2.Location = new Point (300 , 30);t3.Location = new Point (420 ,30); t4.Location = new Point (570 ,30);
t1.Width = 140; t2.Width = 30 ; t3.Width = 65 ; t4.Width = 65;
t1.Text =""; t2.Text =""; t3.Text =""; t4.Text =""; t4.PasswordChar ='*';
t1.Enabled = true ; t2.Enabled = true ; t3.Enabled = true; t4.Enabled = true;
l1.BorderStyle = System.Windows.Forms.BorderStyle.None; l1.ImageIndex =1; l1.ImageAlign = ContentAlignment.TopLeft; l1.UseMnemonic = true;l1.Text = "Remote Server";
l2.BorderStyle = System.Windows.Forms.BorderStyle.None; l2.ImageIndex =1; l2.ImageAlign = ContentAlignment.TopLeft; l2.UseMnemonic = true;l2.Text = "Port#";
l3.BorderStyle = System.Windows.Forms.BorderStyle.None; l3.ImageIndex =1; l3.ImageAlign = ContentAlignment.TopLeft; l3.UseMnemonic = true;l3.Text = "UserName";
l4.BorderStyle = System.Windows.Forms.BorderStyle.None; l4.ImageIndex =1; l4.ImageAlign = ContentAlignment.TopLeft; l4.UseMnemonic = true;l4.Text = "Password";
b1.Text = "Connect";
b2.Text = "Stop";
b1.Click += new EventHandler(Button_Click);
LCHDIR.Click += new System.EventHandler(LC.LocalView_Chdir_Click);
LMKDIR.Click += new System.EventHandler(LC.LocalView_Mkdir_Click);
LRENAME.Click += new System.EventHandler(LC.LocalView_Rename_Click);
LDELETE.Click += new System.EventHandler(LC.LocalView_Delete_Click);
LREFRESH.Click += new System.EventHandler(LC.LocalView_Refresh_Click);
SCHDIR.Click += new System.EventHandler(RC.RemoteView_Chdir_Click);
SMKDIR.Click += new System.EventHandler(RC.RemoteView_Mkdir_Click);
SDELETE.Click += new System.EventHandler(RC.RemoteView_Delete_Click);
SREFRESH.Click += new System.EventHandler(RC.RemoteView_Refresh_Click);
download.Click += new System.EventHandler(TC.Remote_transfer_download);
ListView Lview = LC.addListView();
frm1.AcceptButton =b1;
frm1.AcceptButton =b2;
frm1.Controls.Add(b1);
//frm1.Controls.Add(b2);
frm1.Controls.Add(l1); frm1.Controls.Add(l2); frm1.Controls.Add(l3); frm1.Controls.Add(l4);
frm1.Controls.Add(t1); frm1.Controls.Add(t2); frm1.Controls.Add(t3); frm1.Controls.Add(t4);
frm1.Controls.Add(LCHDIR); frm1.Controls.Add(LMKDIR);
frm1.Controls.Add(upload); frm1.Controls.Add(download);
//frm1.Controls.Add(LRENAME);
frm1.Controls.Add(LDELETE); frm1.Controls.Add(LREFRESH);
frm1.Controls.Add(SCHDIR); frm1.Controls.Add(SMKDIR);
//frm1.Controls.Add(SVIEW); frm1.Controls.Add(SEXECUTE);
//frm1.Controls.Add(SRENAME);
frm1.Controls.Add(SDELETE); frm1.Controls.Add(SREFRESH);
frm1.Controls.Add(SCR);
frm1.Controls.Add(Rview);
frm1.Controls.Add(Lview);
frm1.Activate();
frm1.ShowDialog();
Ftpaccess FAC = new Ftpaccess();
FAC.Connect_Protocol(pro_ser());
Ftptransfer FTP = new Ftptransfer();
}
/* Below class is used for opening data connection with server */
public class Ftptransfer{
public static Ftpaccess FC = new Ftpaccess();
public static FtpLocalControl FL = new FtpLocalControl();
public static FtpRemoteControl FR = new FtpRemoteControl();
public Ftptransfer() {
}
{
String cFileDest = FtpLocalControl.current_path + item.Text.Trim();
String sFile = item.Text.Trim();
FC.sys_put(cFileDest , sFile);
FC.sys_list(FR.pwd);
}
public void Remote_transfer_download(object sender , EventArgs e) {
foreach (ListViewItem item in FtpRemoteControl.RemoteView.SelectedItems)
{
String cFileDest = FtpLocalControl.current_path + item.Text.Trim();
String sFile = item.Text.Trim();
FC.sys_get(cFileDest , sFile);
public class Ftpaccess{
private const String CRLF ="\r\n";
public string acsize;
public static Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
public FtpRemoteControl RC = new FtpRemoteControl();
public static string pwd ="";
public Ftpaccess() {
Console.WriteLine("@-Ftpaccess");
//Console.WriteLine(Connect_Protocol(conn.pro_ser()));
}
IPEndPoint EPhost = new IPEndPoint(hostadd, 21);
String str = null;
if (!s.Connected)
{
str = "";
Console.WriteLine("No Connection set for " + " " + Server );
return str;
}
{
str = "";
Console.W
riteLine("I am connected to " +IPAddress.Parse(((IPEndPoint)s.RemoteEndPoint).Address.ToString()) +
"on port number " + ((IPEndPoint)s.RemoteEndPoint).Port.ToString());
"I am connected on port number " +
((IPEndPoint)s.LocalEndPoint).Port.ToString());
byte[] msg = Encoding.ASCII.GetBytes("");
byte[] welcome = new byte[512];
int i = s.Send(msg, 0, msg.Length, SocketFlags.None);
this.readCommandReply();
if(s.Connected) { User_Login(); Console.WriteLine("Already Connected to Server " + " " + Server); }
if(!s.Connected) { Console.WriteLine("Ftp Tranfer Denied by server " + " " + Server); }
{
public int code;
public string message;
}
public void User_Login() {
byte[] pok = new byte[512];
String USER = "USER " +conn.pro_usr() + CRLF;
String PASS = "PASS " +conn.pro_pass() + CRLF;
s.Send(Encoding.ASCII.GetBytes(USER), USER.Length, 0);
commandReply reply = this.readCommandReply();
s.Receive(uok, 0, s.Available, SocketFlags.None);
s.Send(Encoding.ASCII.GetBytes(PASS), PASS.Length, 0);
commandReply reply1 = this.readCommandReply();
s.Receive(pok, 0, s.Available, SocketFlags.None);
sys_com();
public void sys_com() {
byte[] sok = new byte[512];
String Command = "SYST" + CRLF;
int j= s.Send(Encoding.ASCII.GetBytes(Command), Command.Length, 0);
commandReply SYST = this.readCommandReply();
s.Receive(sok, 0, s.Available, SocketFlags.None);
conn.pro_win(SYST.message);
sys_pwd();
}
byte[] part = new byte[1024];
String Command = "PWD"+ CRLF;
s.Send(Encoding.ASCII.GetBytes(Command), Command.Length, 0);
commandReply PWD = this.readCommandReply();
s.Receive(part, 0, s.Available, SocketFlags.None);
commandReply PWD1 = this.readCommandReply();
conn.server_pwdResponse(PWD.message);
sys_list("");
}
byte[] part = new byte[1024];
String Command = "MKD "+ ndir +"/" + CRLF;
s.Send(Encoding.ASCII.GetBytes(Command), Command.Length, 0);
commandReply PWD = this.readCommandReply();
s.Receive(part, 0, s.Available, SocketFlags.None);
commandReply PWD1 = this.readCommandReply();
conn.pro_win(PWD.message);
//sys_list(RC.pwd);
}
public void sys_type() {
byte[] tyok = new byte[512];
String Command = "TYPE " + "A" + CRLF;
int j= s.Send(Encoding.ASCII.GetBytes(Command), Command.Length, 0);
commandReply TYPE = this.readCommandReply();
s.Receive(tyok, 0, s.Available, SocketFlags.None);
this.readCommandReply();
conn.pro_win(TYPE.message);
sys_list("");
}
Socket dataSocket;
dataSocket = Get_List_Socket();
string command = "TYPE I" + CRLF;
s.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
s.Receive(tyok, 0, s.Available, SocketFlags.None);
commandReply reply = this.readCommandReply();
command = "STOR " + sfile + CRLF;
s.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
s.Receive(tyok, 0, s.Available, SocketFlags.None);
commandReply reply1 = this.readCommandReply();
int sizeReceived;
FileStream fileToRead = new FileStream(cfile, FileMode.Open);
byte[] buffer = new byte[1024];
int bytesRead;
{
bytesRead = fileToRead.Read(buffer,0, 1024);
sizeReceived = dataSocket.Send(buffer, bytesRead, SocketFlags.None);
while (bytesRead == buffer.Length ); // Go back for more if necessary
fileToRead.Close();
dataSocket.Close();
}
byte[] tyok = new byte[512];
Socket dataSocket;
dataSocket = Get_List_Socket();
string command = "TYPE I" + CRLF;
s.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
s.Receive(tyok, 0, s.Available, SocketFlags.None);
commandReply reply = this.readCommandReply();
command="";
command = "RETR " + sFile + CRLF;
s.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
s.Receive(tyok, 0, s.Available, SocketFlags.None);
commandReply reply1 =this.readCommandReply();
acsize = reply1.message.ToString();
int temp1 = acsize.LastIndexOf("(");
int temp2 = acsize.LastIndexOf("b");
acsize = acsize.Substring(temp1+1 , temp2-temp1-1);
ArrayList byteArray = new ArrayList();
int sizeReceived;
int totalbytes =0;
string serverMessage = "";
FileStream oFileStream = new FileStream(cFileDest , FileMode.Create);
do
{
readBytes = new Byte[1024];
sizeReceived = dataSocket.Receive(readBytes, readBytes.Length, SocketFlags.None);
totalbytes = totalbytes + sizeReceived;
serverMessage += Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
oFileStream.W
rite(readBytes,0, sizeReceived);
}
while (totalbytes != System.Convert.ToInt32(acsize));
oFileStream.Close();
reply = this.readCommandReply();
this.readCommandReply();
byte[] tyok = new byte[512];
String Command = "CWD " + pwd + CRLF;
int j= s.Send(Encoding.ASCII.GetBytes(Command), Command.Length, 0);
commandReply TYPE = this.readCommandReply();
s.Receive(tyok, 0, s.Available, SocketFlags.None);
this.readCommandReply();
conn.pro_win(TYPE.message);
sys_list("");
}
public void sys_rmd(String pwd) {
byte[] tyok = new byte[512];
String Command = "RMD " + pwd + CRLF;
int j= s.Send(Encoding.ASCII.GetBytes(Command), Command.Length, 0);
commandReply TYPE = this.readCommandReply();
s.Receive(tyok, 0, s.Available, SocketFlags.None);
this.readCommandReply();
conn.pro_win(TYPE.message);
sys_list("");
}
public void sys_rmf(String pwd) {
byte[] tyok = new byte[512];
String Command = "DELE " + pwd + CRLF;
int j= s.Send(Encoding.ASCII.GetBytes(Command), Command.Length, 0);
commandReply TYPE = this.readCommandReply();
s.Receive(tyok, 0, s.Available, SocketFlags.None);
this.readCommandReply();
conn.pro_win(TYPE.message);
sys_list("");
}
byte[] liok = new byte[1024];
Console.WriteLine("in list");
String Command = "LIST" +Current_pwd + CRLF;
conn.pro_win("PASV");
Socket ListSocket = Get_List_Socket();
conn.pro_win("LIST");
int j= s.Send(Encoding.ASCII.GetBytes(Command), Command.Length, 0);
s.Receive(liok, 0, s.Available, SocketFlags.None);
commandReply TYPE = this.readCommandReply();
s.Receive(liok, 0, s.Available, SocketFlags.None);
{
int sizeReceived;
string serverMessage = "";
do
{
readBytes = new Byte[1024];
sizeReceived = ListSocket.Receive(readBytes, readBytes.Length, SocketFlags.None);
serverMessage += Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
}
while (!serverMessage.EndsWith(CRLF)); // Go back for more if necessary
Console.WriteLine(serverMessage);
pwd = "/home/karcheji/";
Console.WriteLine(s.Connected);
RC.responseListCommand(serverMessage);
}
}
{
Socket localSocket;
byte[] liok = new byte[1024];
string command = "PASV";
command += CRLF;
s.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
Console.WriteLine("I am in List Socket");
s.Receive(liok, 0, s.Available, SocketFlags.None);
commandReply reply = this.readCommandReply();
commandReply reply1 = this.readCommandReply();
string fullAddress = reply.message;
Console.WriteLine("hi" + " " + reply.message);
fullAddress = fullAddress.Remove(0, fullAddress.IndexOf('(') + 1);
fullAddress = fullAddress.Substring(0, fullAddress.IndexOf(')'));
string[] addressParts = fullAddress.Split(',');
string pasvAddress = addressParts[0] + "." + addressParts[1] + "." +addressParts[2] + "." + addressParts[3];
int pasvPort = Convert.ToInt32(addressParts[4]) * 256 + Convert.ToInt32(addressParts[5]);
localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
localSocket.Connect(new IPEndPoint(IPAddress.Parse(pasvAddress), pasvPort));
if(localSocket.Connected)
{
conn.pro_win("Data Channel Connected to" + " " + pasvAddress + " " + pasvPort);
}
return localSocket;
}
public commandReply readCommandReply()
{
reply.code =0;
reply.message ="";
Byte[] readBytes;
bool foundLastLine;
int sizeReceived;
string serverMessage = "";
{
{
readBytes = new Byte[524];
sizeReceived = s.Receive(readBytes);
serverMessage += Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
}
while (sizeReceived == readBytes.Length & !serverMessage.EndsWith(CRLF));
reply.code = Convert.ToInt32(serverMessage.Substring(0,3));
if (serverMessage[3] == '-' & serverMessage.IndexOf(CRLF + reply.code + ' ') == -1)
{
do
{
readBytes = new Byte[524];
sizeReceived = s.Receive(readBytes);
serverMessage += Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
foundLastLine = serverMessage.IndexOf(CRLF + reply.code + ' ') > 0;
}
while (!foundLastLine | !serverMessage.EndsWith(CRLF)); // Go back for more if necessary
}
reply.message = serverMessage.Substring(4,serverMessage.Length-6); // Ignore the <CRLF>
Console.WriteLine(reply.code + " " + reply.message);
}
return reply;
}
public static Ftpaccess FC = new Ftpaccess();
public static Form schdir = new Form();
public static Form smkdir = new Form();
public static TextBox t1 = new TextBox();
public static Button cb1 = new Button();
public static Button cb2 = new Button();
public static TextBox t2 = new TextBox();
public static Button mb1 = new Button();
publi
c static Button mb2 = new Button();
public static Label cl1 = new Label();
public static Label ml1 = new Label();
public static Button rb1 = new Button();
public static Button rb2 = new Button();
public static TextBox rt1 = new TextBox();
private const String CRLF ="\r\n";
public String pwd =" /home/karcheji/";
public FtpRemoteControl() { }
schdir.Size = new Size(400,130);
schdir.Text = "Enter Directory Name : CHDIR";
schdir.HelpButton = true;
schdir.MaximizeBox = false;
schdir.MinimizeBox = false;
schdir.FormBorderStyle = FormBorderStyle.FixedDialog;
smkdir.Size = new Size(400,130);
smkdir.Text = "Enter Directory Name : MKDIR";
smkdir.HelpButton = true;
smkdir.MaximizeBox = false;
smkdir.MinimizeBox = false;
smkdir.FormBorderStyle = FormBorderStyle.FixedDialog;
t2.Location = new Point (80 ,40);
cl1.Location = new Point (80 ,10);
ml1.Location = new Point (80 ,10);
cl1.Width = 130; ml1.Width = 130;
t1.Text =""; t2.Text =""; cl1.Text ="" ; ml1.Text ="";
cb1.Location = new Point(300 , 15); cb1.Text = "OK"; cb1.FlatStyle =FlatStyle.Popup; cb1.BackColor = Color.LightGray;
cb2.Location = new Point(300 , 55); cb2.Text = "Cancel"; cb2.FlatStyle= FlatStyle.Popup; cb2.BackColor = Color.LightGray;
mb1.Location = new Point(300 , 15); mb1.Text = "OK"; mb1.FlatStyle =FlatStyle.Popup; mb1.BackColor = Color.LightGray;
mb2.Location = new Point(300 , 55); mb2.Text = "Cancel"; mb2.FlatStyle= FlatStyle.Popup; mb2.BackColor = Color.LightGray;
cb2.Click += new System.EventHandler(Cdir_CancelClick);
mb1.Click += new System.EventHandler(Mdir_OKClick);
mb2.Click += new System.EventHandler(Mdir_CancelClick);
RemoteView.Bounds = new Rectangle(new Point(425,70), new Size(260,265));
RemoteView.View = View.Details;
RemoteView.LabelEdit = false;
RemoteView.AllowColumnReorder = true;
RemoteView.Scrollable = true;
RemoteView.FullRowSelect = true;
RemoteView.HoverSelection = true;
RemoteView.MultiSelect = true;
RemoteView.Columns.Add("Size", -2, HorizontalAlignment.Center);
RemoteView.Columns.Add("Type", -2, HorizontalAlignment.Center);
ImageList imageListSmall = new ImageList();
ImageList imageListLarge = new ImageList();
imageListSmall.Images.Add(Bitmap.FromFile("C:\clsdfold.bmp"));
imageListSmall.Images.Add(Bitmap.FromFile("C:\openfold.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\openfold.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\clsdfold.bmp"));
RemoteView.LargeImageList = imageListLarge;
RemoteView.SmallImageList = imageListSmall;
}
public void responseListCommand(String response) {
String parser = response + "\r\n";
String[] Response_Parsed;
ImageList imageListFile = new ImageList();
ImageList imageListFolder = new ImageList();
string substr;
Refresh();
Response_Parsed = parser.Split('\r');
} while(!parser.EndsWith("\r\n") | parser==null);
{
substr ="";
String trimstr="";
String Temp1="";
substr =Response_Parsed[i].ToString();
trimstr =Response_Parsed[i].ToString();
substr = "noop";
}
try {
trimstr = trimstr.Remove(0,1);
Console.WriteLine(trimstr);
if(trimstr.StartsWith("-") ) {
imageListFile.Images.Add(Bitmap.FromFile("C:\clsdfold.bmp"));
Temp1 ="File";
}
if(trimstr.StartsWith("d")) {
imageListFolder.Images.Add(Bitmap.FromFile("C:\clsdfold.bmp"));
Temp1 ="Dir";
}
substr = substr.TrimStart(' ');
} catch (ArgumentOutOfRangeException e) {}
RemoteView.Items.Add(addItem(new ListViewItem() , substr , i , Temp1));
}
RemoteView.LargeImageList = imageListFolder;
public void RemoteView_DoubleClick(object sender, System.EventArgs e)
{
String Current_pwd;
foreach (ListViewItem item in FtpRemoteControl.RemoteView.SelectedItems)
{
Current_pwd = item.Text.Trim();
String Command = Current_pwd;
pwd = pwd+Current_pwd+"/";
Console.WriteLine(Ftpaccess.s.Connected);
FC.sys_cwd(Command);
}
}
{
schdir.Controls.Add(cb1);
schdir.Controls.Add(cb2);
schdir.Controls.Add(t1);
schdir.Activate();
schdir.ShowDialog();
Console.WriteLine("hi");
public void RemoteView_Mkdir_Click(object sender, System.EventArgs e)
{
smkdir.Controls.Add(mb1);
smkdir.Controls.Add(mb2);
smkdir.Controls.Add(t2);
smkdir.Activate();
smkdir.ShowDialog();
Console.WriteLine("hi");
public void RemoteView_Refresh_Click(object sender, System.EventArgs e)
{
Console.WriteLine("hi");
public void RemoteView_Delete_Click(object sender, System.EventArgs e)
{
nt_pwd;
foreach (ListViewItem item in FtpRemoteControl.RemoteView.SelectedItems)
{
Current_pwd = item.Text.Trim();
String Command = Current_pwd;
if (item.SubItems[2].ToString().Remove(0, item.SubItems[2].ToString().IndexOf("{")+1).Equals("Dir}"))
{
FC.sys_rmd(Command);
}
else if (item.SubItems[2].ToString().Remove(0,item.SubItems[2].ToString().IndexOf("{")+1).Equals("File}"))
{
FC.sys_rmf(Command);
}
}
{
pwd = pwd+t1.Text.Trim()+"/";
FC.sys_cwd(Command);
schdir.Close();
private void Cdir_CancelClick(object sender, System.EventArgs e)
{
schdir.Close();
}
private void Mdir_OKClick(object sender, System.EventArgs e)
{
FC.sys_mkd(Command);
smkdir.Close();
private void Mdir_CancelClick(object sender, System.EventArgs e)
{
smkdir.Close();
}
{
public int code;
public string message;
}
arList = new ListViewItem(substr ,i);
arList.SubItems.Add("");
arList.SubItems.Add(attr);
return arList;
}
arList = new ListViewItem(substr);
return arList;
}
string cdup ="..";
RemoteView.Clear();
RemoteView.Columns.Add("Name", -2, HorizontalAlignment.Center);
RemoteView.Columns.Add("Size", -2, HorizontalAlignment.Center);
RemoteView.Columns.Add("Type", -2, HorizontalAlignment.Center);
public static ListView LocalView = new ListView();
public static Form fchdir = new Form();
public static Form fmkdir = new Form();
public static Form frename = new Form();
public static TextBox t1 = new TextBox();
public static Button cb1 = new Button();
public static Button cb2 = new Button();
public static TextBox t2 = new TextBox();
public static Button mb1 = new Button();
public static Button mb2 = new Button();
public static Label cl1 = new Label();
public static Label ml1 = new Label();
public static Button rb1 = new Button();
public static Button rb2 = new Button();
public static TextBox rt1 = new TextBox();
public static string current_path = "";
current_path = "c:\";
ImageList imageListFile = new ImageList();
ImageList imageListFolder = new ImageList();
string[] AllFd = Directory.GetDirectories("c:\");
string[] AllFiles = Directory.GetFiles("c:\");
string[] drives = System.IO.Directory.GetLogicalDrives();
string temp = current_dir("");
fchdir.Text = "Enter Directory Name : CHDIR";
fchdir.HelpButton = true;
fchdir.MaximizeBox = false;
fchdir.MinimizeBox = false;
fchdir.FormBorderStyle = FormBorderStyle.FixedDialog;
fmkdir.Size = new Size(400,130);
fmkdir.Text = "Enter Directory Name : MKDIR";
fmkdir.HelpButton = true;
fmkdir.MaximizeBox = false;
fmkdir.MinimizeBox = false;
fmkdir.FormBorderStyle = FormBorderStyle.FixedDialog;
t2.Location = new Point (80 ,40);
cl1.Location = new Point (80 ,10);
ml1.Location = new Point (80 ,10);
cl1.Width = 130; ml1.Width = 130;
t1.Text =""; t2.Text =""; cl1.Text ="" ; ml1.Text ="";
cb1.Location = new Point(300 , 15); cb1.Text = "OK"; cb1.FlatStyle =FlatStyle.Popup; cb1.BackColor = Color.LightGray;
cb2.Location = new Point(300 , 55); cb2.Text = "Cancel"; cb2.FlatStyle= FlatStyle.Popup; cb2.BackColor = Color.LightGray;
mb1.Location = new Point(300 , 15); mb1.Text = "OK"; mb1.FlatStyle =FlatStyle.Popup; mb1.BackColor = Color.LightGray;
mb2.Location = new Point(300 , 55); mb2.Text = "Cancel"; mb2.FlatStyle= FlatStyle.Popup; mb2.BackColor = Color.LightGray;
LocalView.View = View.Details;
LocalView.LabelEdit = false;
LocalView.AllowColumnReorder = true;
LocalView.Scrollable = true;
LocalView.FullRowSelect = true;
LocalView.HoverSelection =true;
LocalView.MultiSelect = true;
cb1.Click += new System.EventHandler(Cdir_OKClick);
cb2.Click += new System.EventHandler(Cdir_CancelClick);
mb1.Click += new System.EventHandler(Mdir_OKClick);
mb2.Click += new System.EventHandler(Mdir_CancelClick);
rb1.Click += new System.EventHandler(Rna_OKClick);
rb2.Click += new System.EventHandler(Rna_CancelClick);
{
} */
{
}
{
//Assign the ImageList objects to the ListView
.
LocalView.LargeImageList = imageListFolder;
LocalView.SmallImageList = imageListFile;
}
ImageList imageListFolder = new ImageList();
string[] AllFiles = Directory.GetFiles(current_dir(path) );
string[] drives = System.IO.Directory.GetLogicalDrives();
string temp = current_dir(path);
current_path=temp + "/";
{
LocalView.SmallImageList.Images.Add(Bitmap.FromFile("c:\up.gif"));
LocalView.LargeImageList.Images.Add(Bitmap.FromFile("c:\isfile.bmp"));
}
{
LocalView.View = View.Details;
{
imageListFile.Images.Add(Bitmap.FromFile("c:\openfold.bmp"));
imageListFolder.Images.Add(Bitmap.FromFile("c:\openfold.bmp"));
imageListFolder.Images.Add(Bitmap.FromFile("c:\openfold.bmp"));
LocalView.LargeImageList = imageListFolder;
//LocalView.SmallImageList = imageListFile;
ImageList imageListFolder = new ImageList();
string[] AllFd = Directory.GetFileSystemEntries(get_parent(current_path) , "*.*");
string temp = get_parent(current_path);
{
imageListFile.Images.Add(Bitmap.FromFile("C:\isfile.bmp"));
imageListFolder.Images.Add(Bitmap.FromFile("C:\isfile.bmp"));
imageListFolder.Images.Add(Bitmap.FromFile("C:\isfile.bmp"));
LocalView.LargeImageList = imageListFolder;
LocalView.SmallImageList = imageListFile;
}
private void LocalView_DoubleClick(object sender, System.EventArgs e)
{
foreach (ListViewItem item in LocalView.SelectedItems)
{
Console.WriteLine(item.Text.Trim());
getListView(item.Text.Trim());
}
public void LocalView_Chdir_Click(object sender, System.EventArgs e)
{
fchdir.Controls.Add(cb1);
fchdir.Controls.Add(cb2);
fchdir.Controls.Add(t1);
fchdir.Activate();
fchdir.ShowDialog();
Console.WriteLine("hi");
public void LocalView_Mkdir_Click(object sender, System.EventArgs e)
{
fmkdir.Controls.Add(mb1);
fmkdir.Controls.Add(mb2);
fmkdir.Controls.Add(t2);
fmkdir.Activate();
fmkdir.ShowDialog();
Console.WriteLine("hi");
public void LocalView_Refresh_Click(object sender, System.EventArgs e)
{
getListView(" ");
Console.WriteLine("hi");
{
frename.Text = "Enter File or Directory Name";
frename.HelpButton = true;
frename.MaximizeBox = false;
frename.MinimizeBox = false;
frename.FormBorderStyle = FormBorderStyle.FixedDialog;
rt1.Location = new Point (80 ,40) ;
rt1.Width = 130;
rb1.Location = new Point(300 , 15); rb1.Text = "OK"; rb1.FlatStyle =FlatStyle.Popup; rb1.BackColor = Color.LightGray;
rb2.Location = new Point(300 , 55); rb2.Text = "Cancel"; rb2.FlatStyle= FlatStyle.Popup; rb2.BackColor = Color.LightGray;
frename.Controls.Add(rb1);
frename.Controls.Add(rb2);
frename.Controls.Add(rt1);
frename.Activate();
frename.ShowDialog();
Console.WriteLine("hi");
frename.Close();
}
private void Rna_CancelClick(object sender, System.EventArgs e) {
frename.Close();
}
{
{
if (item.SubItems[2].ToString().Remove(0, item.SubItems[2].ToString().IndexOf("{")+1).Equals("Dir}")) {
System.IO.Directory.Delete(current_path+item.Text.Trim(), false);
} catch(System.IO.IOException ) { main.pro_win("Directory cannot be deleted : Dir Not empty");}
else if (item.SubItems[2].ToString().Remove(0,item.SubItems[2].ToString().IndexOf("{")+1).Equals("File}")) {System.IO.File.Delete(current_path+item.Text.Trim());
Console.WriteLine(item.SubItems[2].ToString());}
Console.WriteLine(item.SubItems[2].ToString().Remove(0, item.SubItems[2].ToString().IndexOf("{")));
}
getListView(" ");
private void Cdir_OKClick(object sender, System.EventArgs e)
{
if(System.IO.Directory.Exists(current_path+t1.Text.Trim()) == false) {
main.pro_win("Error: Cannot create : Directory does not Exist " + " " + t1.Text.Trim());
fchdir.Close();
}
if(System.IO.Directory.Exists(current_path+t1.Text.Trim()) == true) {
getListView(t1.T
ext.Trim());
fchdir.Close();
}
private void Cdir_CancelClick(object sender, System.EventArgs e)
{
fchdir.Close();
}
private void Mdir_OKClick(object sender, System.EventArgs e)
{
if(System.IO.Directory.Exists(current_path+t2.Text.Trim()) == true) {
main.pro_win("Error: Cannot create : Directory Allready Exists " + " " + t2.Text.Trim());
fmkdir.Close();
}
if(System.IO.Directory.Exists(current_path+t1.Text.Trim()) == false) {
System.IO.Directory.CreateDirectory(current_path+ t2.Text.Trim());
getListView(" ");
fmkdir.Close();
}
}
private void Mdir_CancelClick(object sender, System.EventArgs e)
{
fmkdir.Close();
}
string cdup ="..";
LocalView.Clear();
LocalView.Columns.Add("Name", -2, HorizontalAlignment.Center);
LocalView.Columns.Add("Size", -2, HorizontalAlignment.Center);
LocalView.Columns.Add("Type", -2, HorizontalAlignment.Center);
}
private ListViewItem daddItem(ListViewItem arList , String substr) {
arList = new ListViewItem(substr);
arList.SubItems.Add("");
if (substr.Equals("A")) { arList.SubItems.Add("Floppy Disk");}
else if (substr.Equals("C")) { arList.SubItems.Add("Hard Disk");}
else if (substr.Equals("D")) { arList.SubItems.Add("CD-ROM");}
else {arList.SubItems.Add("Dir");}
return arList;
}
private ListViewItem faddItem(ListViewItem arList , String substr) {
arList = new ListViewItem(substr);
FileInfo size = new FileInfo(current_path+substr);
arList.SubItems.Add(size.Length.ToString());
arList.SubItems.Add("File");
return arList;
}
private string current_dir(string path){
string set="";
set = current_path+path;
{set = "c:\ ";
Console.WriteLine("Am i in c");}
{set = "d:\ ";
Console.WriteLine("Am i in d");}
{set = "a:\ ";
Console.WriteLine("Am i in a");}
}
string parentpath="";
try {
DirectoryInfo directoryInfo = Directory.GetParent(path);
parentpath = directoryInfo.FullName;
Console.WriteLine(directoryInfo.FullName);
}
catch (ArgumentNullException) {
System.Console.WriteLine("Path is a null reference.");
}
catch (ArgumentException) {
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, or " +
"contains invalid characters.");
}




25. Oct, 2006 by 







No comments yet... Be the first to leave a reply!