由于项目需要,简单的写了歌上传文件到ftp的程序.下载以及列出文件列表等还没有时间写,由于项目不需要,就没有.如果以后有机会的话再写吧.希望能对您有所参考价值.
FTP类
using System;
using System.Net.Sockets;
using System.Net.Sockets;
namespace
MysticBoy
{
/// <summary>
/// Ftp 的摘要说明。
/// </summary>
public class Ftp
{
public Ftp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
string _ser;
public string Server
{
set{ _ser=value;}
get{return _ser;}
}
string _port;
public string Port
{
set{_port=value;}
get{return _port;}
}
string _user;
public string User
{
set{ _user=value;}
get{return _user;}
}
string _pws;
public string Password
{
set{ _pws=value;}
get{return _pws;}
}
bool _IsLog;
public bool IsLogined
{
set{ _IsLog=value;}
get{return _IsLog;}
}
System.Windows.Forms.Label _infx=new System.Windows.Forms.Label() ;
public System.Windows.Forms.Label InfsShow
{
set{_infx=value;}
get{return _infx;}
}
System.Windows.Forms.ListBox _infs=new System.Windows.Forms.ListBox();
public System.Windows.Forms.ListBox InfsList
{
set{_infs=value;}
get{return _infs;}
}
{
/// <summary>
/// Ftp 的摘要说明。
/// </summary>
public class Ftp
{
public Ftp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
string _ser;
public string Server
{
set{ _ser=value;}
get{return _ser;}
}
string _port;
public string Port
{
set{_port=value;}
get{return _port;}
}
string _user;
public string User
{
set{ _user=value;}
get{return _user;}
}
string _pws;
public string Password
{
set{ _pws=value;}
get{return _pws;}
}
bool _IsLog;
public bool IsLogined
{
set{ _IsLog=value;}
get{return _IsLog;}
}
System.Windows.Forms.Label _infx=new System.Windows.Forms.Label() ;
public System.Windows.Forms.Label InfsShow
{
set{_infx=value;}
get{return _infx;}
}
System.Windows.Forms.ListBox _infs=new System.Windows.Forms.ListBox();
public System.Windows.Forms.ListBox InfsList
{
set{_infs=value;}
get{return _infs;}
}
public string NowInf;
System.Net.Sockets.TcpClient tcFtp;
NetworkStream Stm;
public bool Login()
{
try
{
SetInf("正在连接服务器...");
tcFtp=new TcpClient(_ser, System.Convert.ToInt32(_port));//连接服务器
Stm= tcFtp.GetStream();//获取网络流
byte[] data=new byte[255];
int bytes;
SetInf("等待服务器就绪!");
bytes=Stm.Read(data,0,data.Length);
string msg=System.Text.Encoding.ASCII.GetString(data,0,bytes);//读取登录成功的消息
if (msg.Substring(0,3)=="220")
{
SetInf(msg);
if (DoCmd("USER " + _user+" ","331"))
{
if (DoCmd("PASS " + _pws,"230"))
{
SetInf("服务器就绪!");
this.IsLogined=true;
return true;
}
SetInf("密码错误!");
return false;
}
SetInf("用户名错误");
return false;
}
else
{
SetInf("服务器没有正确响应!");
return false;
}
System.Net.Sockets.TcpClient tcFtp;
NetworkStream Stm;
public bool Login()
{
try
{
SetInf("正在连接服务器...");
tcFtp=new TcpClient(_ser, System.Convert.ToInt32(_port));//连接服务器
Stm= tcFtp.GetStream();//获取网络流
byte[] data=new byte[255];
int bytes;
SetInf("等待服务器就绪!");
bytes=Stm.Read(data,0,data.Length);
string msg=System.Text.Encoding.ASCII.GetString(data,0,bytes);//读取登录成功的消息
if (msg.Substring(0,3)=="220")
{
SetInf(msg);
if (DoCmd("USER " + _user+" ","331"))
{
if (DoCmd("PASS " + _pws,"230"))
{
SetInf("服务器就绪!");
this.IsLogined=true;
return true;
}
SetInf("密码错误!");
return false;
}
SetInf("用户名错误");
return false;
}
else
{
SetInf("服务器没有正确响应!");
return false;
}
}
catch (System.Exception ex)
{
NowInf=ex.Message ;
_infx.Text=NowInf;
return false;
}
}
/// <summary>
///
/// </summary>
/// <param name="cmd"></param>
/// <param name="wait">匹配前面的字符就可以了</param>
/// <returns></returns>
private bool DoCmd(string cmd,string wait)
{
string msg=DoCommand(cmd);
SetInf(msg);
return (msg.Substring(0,wait.Length).ToLower()==wait.ToLower() );//如果msg的前部分是wait,则返回成功.
}
private bool RetOk(string s1,string s2)
{
return (s1.Substring(0,s2.Length).ToLower()==s2.ToLower() );//如果msg的前部分是wait,则返回成功.
}
private string DoCommand(string cmd)
{
string msg=cmd;//+System.Text.Encoding.ASCII.GetString(vbnull);
byte[] data=System.Text.Encoding.ASCII.GetBytes(msg+"/r/n");
Stm.Write(data,0,data.Length);
///
/// </summary>
/// <param name="cmd"></param>
/// <param name="wait">匹配前面的字符就可以了</param>
/// <returns></returns>
private bool DoCmd(string cmd,string wait)
{
string msg=DoCommand(cmd);
SetInf(msg);
return (msg.Substring(0,wait.Length).ToLower()==wait.ToLower() );//如果msg的前部分是wait,则返回成功.
}
private bool RetOk(string s1,string s2)
{
return (s1.Substring(0,s2.Length).ToLower()==s2.ToLower() );//如果msg的前部分是wait,则返回成功.
}
private string DoCommand(string cmd)
{
string msg=cmd;//+System.Text.Encoding.ASCII.GetString(vbnull);
byte[] data=System.Text.Encoding.ASCII.GetBytes(msg+"/r/n");
Stm.Write(data,0,data.Length);
data=new byte[255];
int bytes;
while(Stm.DataAvailable==false)
{
System.Windows.Forms.Application.DoEvents() ;
NowInf="正在等待服务器响应!";
}
bytes=Stm.Read(data,0,data.Length);
msg=System.Text.Encoding.ASCII.GetString(data,0,bytes);
return msg;
}
private void SetInf(string msg)
{
msg+=" "+ System.DateTime.Now.TimeOfDay.ToString();
NowInf=msg;
_infx.Text=NowInf;
_infs.Items.Add(msg);
System.Windows.Forms.Application.DoEvents();
_infs.SelectedIndex=_infs.Items.Count-1;
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
}
int bytes;
while(Stm.DataAvailable==false)
{
System.Windows.Forms.Application.DoEvents() ;
NowInf="正在等待服务器响应!";
}
bytes=Stm.Read(data,0,data.Length);
msg=System.Text.Encoding.ASCII.GetString(data,0,bytes);
return msg;
}
private void SetInf(string msg)
{
msg+=" "+ System.DateTime.Now.TimeOfDay.ToString();
NowInf=msg;
_infx.Text=NowInf;
_infs.Items.Add(msg);
System.Windows.Forms.Application.DoEvents();
_infs.SelectedIndex=_infs.Items.Count-1;
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
}
private string GetMsg()
{
string msg;
byte[] data=new byte[1024];
int bytes;
while(Stm.DataAvailable==false)//如果没有信息可读,等待服务器响应.
{
System.Windows.Forms.Application.DoEvents() ;
}
bytes=Stm.Read(data,0,data.Length);//读取数据
msg=System.Text.Encoding.ASCII.GetString(data,0,bytes);//转换为字符
int rn= msg.IndexOf("/r/n",0);//查找换行符号
if (rn>0)
{
string mg=msg.Substring(rn);//这里是为了防止传输来多行数据,我试图加大缓冲,也是这样.
if( mg.Length>2)//在这里处理如同226这样消息.
{ //前面一次返回接受了一部分,第二次接受到了前面的一部分,在这里用来分开他们
mg=mg.Substring(2);
msg=mg;
}
}
SetInf(msg);
return msg;
}
/// <summary>
/// 注销登录
/// </summary>
/// <returns>成功时返回真</returns>
public bool Logout()
{
this.IsLogined=false;
return DoCmd("QUIT","221");
}
public bool SetCurDir(string Path)
{
if (DoCmd("CWD "+Path,"250")==false)//Directory changed to /SvrPath
{
SetInf("设置当前目录失败!");
return false;
}
if (DoCmd("PWD "+Path,"257")==false)//"" is current directory.
{
SetInf("确认当前目录失败!");
return false;
}
return true;
}
{
string msg;
byte[] data=new byte[1024];
int bytes;
while(Stm.DataAvailable==false)//如果没有信息可读,等待服务器响应.
{
System.Windows.Forms.Application.DoEvents() ;
}
bytes=Stm.Read(data,0,data.Length);//读取数据
msg=System.Text.Encoding.ASCII.GetString(data,0,bytes);//转换为字符
int rn= msg.IndexOf("/r/n",0);//查找换行符号
if (rn>0)
{
string mg=msg.Substring(rn);//这里是为了防止传输来多行数据,我试图加大缓冲,也是这样.
if( mg.Length>2)//在这里处理如同226这样消息.
{ //前面一次返回接受了一部分,第二次接受到了前面的一部分,在这里用来分开他们
mg=mg.Substring(2);
msg=mg;
}
}
SetInf(msg);
return msg;
}
/// <summary>
/// 注销登录
/// </summary>
/// <returns>成功时返回真</returns>
public bool Logout()
{
this.IsLogined=false;
return DoCmd("QUIT","221");
}
public bool SetCurDir(string Path)
{
if (DoCmd("CWD "+Path,"250")==false)//Directory changed to /SvrPath
{
SetInf("设置当前目录失败!");
return false;
}
if (DoCmd("PWD "+Path,"257")==false)//"" is current directory.
{
SetInf("确认当前目录失败!");
return false;
}
return true;
}
public bool Delete(string FileName)
{
return DoCmd("DELE "+FileName,"250");
}
{
return DoCmd("DELE "+FileName,"250");
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="FileName">要上传的本地文件</param>
/// <param name="SvrPath">服务器目标路径.</param>
/// <returns>传输成功返回真.</returns>
public bool UploadFile(string FileName,string SvrPath)
{
if (this.IsLogined==false)
{
SetInf("没有连接到服务器或没有登录!");
return false;
}
if (DoCmd("CWD "+SvrPath,"250")==false)//Directory changed to /SvrPath
{
SetInf("设置当前目录失败!");
return false;
}
if (DoCmd("PWD "+SvrPath,"257")==false)//"" is current directory.
{
SetInf("确认当前目录失败!");
return false;
}
if (DoCmd("TYPE I","200")==false)//Type set to I.
{
SetInf("执行Type i失败!");
return false;
}
string[] pt=FileName.Split("//".ToCharArray());
string filename=pt[pt.Length-1] ;//SvrPath+"/"+pt[pt.Length-1] ;
string tmp1=DoCommand("SIZE "+ filename);
SetInf(tmp1);
if (RetOk(tmp1,"550")!=true)//50 /bbs/Dv_ForumNews/广告.txt: No such file.
{
SetInf("文件已存在!");
return false;
}
if (DoCmd("MODE S" ,"200")==false)//Type set to I.
{
SetInf("模式设置失败!");
return false;
}
/获取传输文件的ip和段口
string tmp2 =DoCommand("PASV");
SetInf(tmp2);
if (RetOk(tmp2,"227")==false)//Type set to I.
{
SetInf("服务器没有返回227,无法知道端口,也无法开始传输文件!");
return false;
}
string[] ips=GetIP( tmp2);
///
TcpClient tcm;
try
{
SetInf("正在连接文件传输服务器!");
tcm=new TcpClient(ips[0],System.Convert.ToInt32(ips[1]));//连接传输服务器.
}
catch (System.Exception ex1)
{
SetInf("连接文件传输服务器时出错:IP地址:"+ips[0]+"端口:" + ips[1]+" 相关信息:" + ex1.Message );
return false;
}
if (DoCmd("STOR "+ filename,"150")==false)//发送传输文件的指令.返回应该是150,否则出错
{
SetInf("开始传输文件的指令服务器响应的不是150,不知道服务器是不是准备接受文件!");
return false;
}
//读取文件///
SetInf("正在读取文件,准备传输...");
System.IO.FileStream fs;
byte[] fbins;
try
{
fs=new System.IO.FileStream(FileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
fbins=new byte[fs.Length] ;
fs.Read(fbins,0,System.Convert.ToInt32(fs.Length));
}
catch( System.Exception ex2)
{
SetInf("文件读取失败,失败原因:"+ex2.Message);
return false;
}
/
NetworkStream tmx=tcm.GetStream();//获得流
SetInf("正在向网络数据流中写入数据...");
tmx.Write(fbins,0,fbins.Length);//写入信息
tmx=null;
tcm.Close();
if (RetOk(GetMsg(),"226")==false)//一般226信息为两次 ,本次是说明磁盘目前可用大小已经使用大小
{
}
string tmp3;
tmp3=DoCommand("SIZE "+ filename);//获取大小.
SetInf(tmp3);
if (RetOk(tmp3,"213"))//返回大小
{
SetInf("正在核对文件大小");
int ln=System.Convert.ToInt32(tmp3.Substring(4));//得到远程文件的大小
if( ln==fbins.Length)//如果大小相同.说明完整传输.
{
SetInf("文件传输成功!");
return true;
}
SetInf("文件传输后文件大小不一致!");
return false;
}
SetInf("文件传输成功,但确认文件大小时服务器响应的不是213!因此无法保证数据传输是否正确!");
return false;
}
/// 上传文件
/// </summary>
/// <param name="FileName">要上传的本地文件</param>
/// <param name="SvrPath">服务器目标路径.</param>
/// <returns>传输成功返回真.</returns>
public bool UploadFile(string FileName,string SvrPath)
{
if (this.IsLogined==false)
{
SetInf("没有连接到服务器或没有登录!");
return false;
}
if (DoCmd("CWD "+SvrPath,"250")==false)//Directory changed to /SvrPath
{
SetInf("设置当前目录失败!");
return false;
}
if (DoCmd("PWD "+SvrPath,"257")==false)//"" is current directory.
{
SetInf("确认当前目录失败!");
return false;
}
if (DoCmd("TYPE I","200")==false)//Type set to I.
{
SetInf("执行Type i失败!");
return false;
}
string[] pt=FileName.Split("//".ToCharArray());
string filename=pt[pt.Length-1] ;//SvrPath+"/"+pt[pt.Length-1] ;
string tmp1=DoCommand("SIZE "+ filename);
SetInf(tmp1);
if (RetOk(tmp1,"550")!=true)//50 /bbs/Dv_ForumNews/广告.txt: No such file.
{
SetInf("文件已存在!");
return false;
}
if (DoCmd("MODE S" ,"200")==false)//Type set to I.
{
SetInf("模式设置失败!");
return false;
}
/获取传输文件的ip和段口
string tmp2 =DoCommand("PASV");
SetInf(tmp2);
if (RetOk(tmp2,"227")==false)//Type set to I.
{
SetInf("服务器没有返回227,无法知道端口,也无法开始传输文件!");
return false;
}
string[] ips=GetIP( tmp2);
///
TcpClient tcm;
try
{
SetInf("正在连接文件传输服务器!");
tcm=new TcpClient(ips[0],System.Convert.ToInt32(ips[1]));//连接传输服务器.
}
catch (System.Exception ex1)
{
SetInf("连接文件传输服务器时出错:IP地址:"+ips[0]+"端口:" + ips[1]+" 相关信息:" + ex1.Message );
return false;
}
if (DoCmd("STOR "+ filename,"150")==false)//发送传输文件的指令.返回应该是150,否则出错
{
SetInf("开始传输文件的指令服务器响应的不是150,不知道服务器是不是准备接受文件!");
return false;
}
//读取文件///
SetInf("正在读取文件,准备传输...");
System.IO.FileStream fs;
byte[] fbins;
try
{
fs=new System.IO.FileStream(FileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
fbins=new byte[fs.Length] ;
fs.Read(fbins,0,System.Convert.ToInt32(fs.Length));
}
catch( System.Exception ex2)
{
SetInf("文件读取失败,失败原因:"+ex2.Message);
return false;
}
/
NetworkStream tmx=tcm.GetStream();//获得流
SetInf("正在向网络数据流中写入数据...");
tmx.Write(fbins,0,fbins.Length);//写入信息
tmx=null;
tcm.Close();
if (RetOk(GetMsg(),"226")==false)//一般226信息为两次 ,本次是说明磁盘目前可用大小已经使用大小
{
}
string tmp3;
tmp3=DoCommand("SIZE "+ filename);//获取大小.
SetInf(tmp3);
if (RetOk(tmp3,"213"))//返回大小
{
SetInf("正在核对文件大小");
int ln=System.Convert.ToInt32(tmp3.Substring(4));//得到远程文件的大小
if( ln==fbins.Length)//如果大小相同.说明完整传输.
{
SetInf("文件传输成功!");
return true;
}
SetInf("文件传输后文件大小不一致!");
return false;
}
SetInf("文件传输成功,但确认文件大小时服务器响应的不是213!因此无法保证数据传输是否正确!");
return false;
}
/// <summary>
/// 从消息里提取用于传输文件的服务器地址和端口
/// </summary>
/// <param name="Inf">消息</param>
/// <returns>数组,0为ip地址,1为端口.</returns>
private string[] GetIP(string Inf)
{
string[] rt=new string[2];
if (Inf.IndexOf("(")>0 )
{
string ips=Inf.Substring( Inf.IndexOf("(")+1);
ips=ips.Substring(0,ips.IndexOf(")"));
string[] ip=ips.Split(",".ToCharArray());
rt[0]=ip[0]+"."+ip[1]+"."+ip[2]+"."+ip[3];//前面4组byte是ip地址.
int x1=System.Convert.ToInt16(ip[4]);//ip地址
int x2=System.Convert.ToInt16(ip[5]);//端口
int rtx=x1*256+x2;//计算端口.消息里后两组byte是端口,前者乘256加上后者就是服务器给你的端口.
rt[1]=rtx.ToString();
}
return rt;
}
}
}
/// 从消息里提取用于传输文件的服务器地址和端口
/// </summary>
/// <param name="Inf">消息</param>
/// <returns>数组,0为ip地址,1为端口.</returns>
private string[] GetIP(string Inf)
{
string[] rt=new string[2];
if (Inf.IndexOf("(")>0 )
{
string ips=Inf.Substring( Inf.IndexOf("(")+1);
ips=ips.Substring(0,ips.IndexOf(")"));
string[] ip=ips.Split(",".ToCharArray());
rt[0]=ip[0]+"."+ip[1]+"."+ip[2]+"."+ip[3];//前面4组byte是ip地址.
int x1=System.Convert.ToInt16(ip[4]);//ip地址
int x2=System.Convert.ToInt16(ip[5]);//端口
int rtx=x1*256+x2;//计算端口.消息里后两组byte是端口,前者乘256加上后者就是服务器给你的端口.
rt[1]=rtx.ToString();
}
return rt;
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace RailWay
{
public class FrmFtp : RailWay.FrmBaseForm
{
private System.Windows.Forms.Button btnUpLoad;
private System.Windows.Forms.TextBox txtFile;
private System.Windows.Forms.TextBox txtPws;
private System.Windows.Forms.TextBox txtUser;
private System.Windows.Forms.TextBox txtFTP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button btnLiuLan;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Button btnDel;
private System.Windows.Forms.Button btnClose;
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Label lblInf;
private System.Windows.Forms.ListBox lstInfs;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtSvrPath;
private System.Windows.Forms.OpenFileDialog dlgOpen;
private RailWay.Ftp ftp=new Ftp();
public FrmFtp()
{
// 该调用是 Windows 窗体设计器所必需的。
InitializeComponent();
{
public class FrmFtp : RailWay.FrmBaseForm
{
private System.Windows.Forms.Button btnUpLoad;
private System.Windows.Forms.TextBox txtFile;
private System.Windows.Forms.TextBox txtPws;
private System.Windows.Forms.TextBox txtUser;
private System.Windows.Forms.TextBox txtFTP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button btnLiuLan;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Button btnDel;
private System.Windows.Forms.Button btnClose;
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Label lblInf;
private System.Windows.Forms.ListBox lstInfs;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtSvrPath;
private System.Windows.Forms.OpenFileDialog dlgOpen;
private RailWay.Ftp ftp=new Ftp();
public FrmFtp()
{
// 该调用是 Windows 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblInf = new System.Windows.Forms.Label();
this.btnUpLoad = new System.Windows.Forms.Button();
this.txtFile = new System.Windows.Forms.TextBox();
this.txtPws = new System.Windows.Forms.TextBox();
this.txtUser = new System.Windows.Forms.TextBox();
this.txtFTP = new System.Windows.Forms.TextBox();
this.btnLiuLan = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txtPort = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.btnDel = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.lstInfs = new System.Windows.Forms.ListBox();
this.btnLogin = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.txtSvrPath = new System.Windows.Forms.TextBox();
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// lblInf
//
this.lblInf.Location = new System.Drawing.Point(0, 368);
this.lblInf.Name = "lblInf";
this.lblInf.Size = new System.Drawing.Size(456, 16);
this.lblInf.TabIndex = 11;
this.lblInf.Text = "就绪";
this.lblInf.Click += new System.EventHandler(this.label1_Click);
//
// btnUpLoad
//
this.btnUpLoad.Location = new System.Drawing.Point(352, 168);
this.btnUpLoad.Name = "btnUpLoad";
this.btnUpLoad.Size = new System.Drawing.Size(72, 23);
this.btnUpLoad.TabIndex = 10;
this.btnUpLoad.Text = "开始上传";
this.btnUpLoad.Click += new System.EventHandler(this.btnUpLoad_Click);
//
// txtFile
//
this.txtFile.Location = new System.Drawing.Point(24, 128);
this.txtFile.Name = "txtFile";
this.txtFile.Size = new System.Drawing.Size(312, 21);
this.txtFile.TabIndex = 9;
this.txtFile.Text = "";
//
// txtPws
//
this.txtPws.Location = new System.Drawing.Point(264, 72);
this.txtPws.Name = "txtPws";
this.txtPws.PasswordChar = '*';
this.txtPws.Size = new System.Drawing.Size(80, 21);
this.txtPws.TabIndex = 8;
this.txtPws.Text = "";
//
// txtUser
//
this.txtUser.Location = new System.Drawing.Point(72, 72);
this.txtUser.Name = "txtUser";
this.txtUser.Size = new System.Drawing.Size(136, 21);
this.txtUser.TabIndex = 7;
this.txtUser.Text = "";
//
// txtFTP
//
this.txtFTP.Location = new System.Drawing.Point(80, 16);
this.txtFTP.Name = "txtFTP";
this.txtFTP.Size = new System.Drawing.Size(128, 21);
this.txtFTP.TabIndex = 6;
this.txtFTP.Text = "";
//
// btnLiuLan
//
this.btnLiuLan.Location = new System.Drawing.Point(352, 128);
this.btnLiuLan.Name = "btnLiuLan";
this.btnLiuLan.TabIndex = 12;
this.btnLiuLan.Text = "浏览...";
this.btnLiuLan.Click += new System.EventHandler(this.btnLiuLan_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 13;
this.label2.Text = "服务器地址";
//
// label3
//
this.label3.Location = new System.Drawing.Point(224, 16);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 14;
this.label3.Text = "服务器端口";
this.label3.Click += new System.EventHandler(this.label3_Click);
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(296, 16);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(48, 21);
this.txtPort.TabIndex = 15;
this.txtPort.Text = "21";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(48, 23);
this.label4.TabIndex = 16;
this.label4.Text = "用户名:";
//
// label5
//
this.label5.Location = new System.Drawing.Point(216, 72);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(48, 23);
this.label5.TabIndex = 17;
this.label5.Text = "口令:";
//
// label6
//
this.label6.Location = new System.Drawing.Point(24, 104);
this.label6.Name = "label6";
this.label6.TabIndex = 18;
this.label6.Text = "要上传的文件";
//
// label7
//
this.label7.Location = new System.Drawing.Point(80, 40);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(240, 23);
this.label7.TabIndex = 19;
this.label7.Text = "例如:192.168.0.1 或 ftp.xjftp.com";
//
// btnDel
//
this.btnDel.ForeColor = System.Drawing.Color.Black;
this.btnDel.Location = new System.Drawing.Point(24, 208);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(96, 23);
this.btnDel.TabIndex = 20;
this.btnDel.Text = "删除相同文件";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(352, 208);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 21;
this.btnClose.Text = "关闭";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// lstInfs
//
this.lstInfs.ItemHeight = 12;
this.lstInfs.Location = new System.Drawing.Point(16, 240);
this.lstInfs.Name = "lstInfs";
this.lstInfs.Size = new System.Drawing.Size(416, 112);
this.lstInfs.TabIndex = 22;
this.lstInfs.SelectedIndexChanged += new System.EventHandler(this.lstInfs_SelectedIndexChanged);
//
// btnLogin
//
this.btnLogin.Location = new System.Drawing.Point(352, 72);
this.btnLogin.Name = "btnLogin";
this.btnLogin.TabIndex = 23;
this.btnLogin.Text = "登录";
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 152);
this.label1.Name = "label1";
this.label1.TabIndex = 24;
this.label1.Text = "服务器路径:";
//
// txtSvrPath
//
this.txtSvrPath.Location = new System.Drawing.Point(24, 168);
this.txtSvrPath.Name = "txtSvrPath";
this.txtSvrPath.Size = new System.Drawing.Size(304, 21);
this.txtSvrPath.TabIndex = 25;
this.txtSvrPath.Text = "/";
this.txtSvrPath.TextChanged += new System.EventHandler(this.txtSvrPath_TextChanged);
//
// dlgOpen
//
this.dlgOpen.Filter = "所有文件(*.*)|*.*";
this.dlgOpen.Title = "打开文件";
//
// FrmFtp
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(466, 408);
this.Controls.Add(this.txtSvrPath);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.txtFile);
this.Controls.Add(this.txtPws);
this.Controls.Add(this.txtUser);
this.Controls.Add(this.txtFTP);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.lstInfs);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnDel);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnLiuLan);
this.Controls.Add(this.lblInf);
this.Controls.Add(this.btnUpLoad);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "FrmFtp";
this.Text = "文件上传";
this.Controls.SetChildIndex(this.btnUpLoad, 0);
this.Controls.SetChildIndex(this.lblInf, 0);
this.Controls.SetChildIndex(this.btnLiuLan, 0);
this.Controls.SetChildIndex(this.label2, 0);
this.Controls.SetChildIndex(this.label3, 0);
this.Controls.SetChildIndex(this.label4, 0);
this.Controls.SetChildIndex(this.label5, 0);
this.Controls.SetChildIndex(this.label6, 0);
this.Controls.SetChildIndex(this.label7, 0);
this.Controls.SetChildIndex(this.btnDel, 0);
this.Controls.SetChildIndex(this.btnClose, 0);
this.Controls.SetChildIndex(this.lstInfs, 0);
this.Controls.SetChildIndex(this.btnLogin, 0);
this.Controls.SetChildIndex(this.label1, 0);
this.Controls.SetChildIndex(this.txtFTP, 0);
this.Controls.SetChildIndex(this.txtUser, 0);
this.Controls.SetChildIndex(this.txtPws, 0);
this.Controls.SetChildIndex(this.txtFile, 0);
this.Controls.SetChildIndex(this.txtPort, 0);
this.Controls.SetChildIndex(this.txtSvrPath, 0);
this.ResumeLayout(false);
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblInf = new System.Windows.Forms.Label();
this.btnUpLoad = new System.Windows.Forms.Button();
this.txtFile = new System.Windows.Forms.TextBox();
this.txtPws = new System.Windows.Forms.TextBox();
this.txtUser = new System.Windows.Forms.TextBox();
this.txtFTP = new System.Windows.Forms.TextBox();
this.btnLiuLan = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txtPort = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.btnDel = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.lstInfs = new System.Windows.Forms.ListBox();
this.btnLogin = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.txtSvrPath = new System.Windows.Forms.TextBox();
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// lblInf
//
this.lblInf.Location = new System.Drawing.Point(0, 368);
this.lblInf.Name = "lblInf";
this.lblInf.Size = new System.Drawing.Size(456, 16);
this.lblInf.TabIndex = 11;
this.lblInf.Text = "就绪";
this.lblInf.Click += new System.EventHandler(this.label1_Click);
//
// btnUpLoad
//
this.btnUpLoad.Location = new System.Drawing.Point(352, 168);
this.btnUpLoad.Name = "btnUpLoad";
this.btnUpLoad.Size = new System.Drawing.Size(72, 23);
this.btnUpLoad.TabIndex = 10;
this.btnUpLoad.Text = "开始上传";
this.btnUpLoad.Click += new System.EventHandler(this.btnUpLoad_Click);
//
// txtFile
//
this.txtFile.Location = new System.Drawing.Point(24, 128);
this.txtFile.Name = "txtFile";
this.txtFile.Size = new System.Drawing.Size(312, 21);
this.txtFile.TabIndex = 9;
this.txtFile.Text = "";
//
// txtPws
//
this.txtPws.Location = new System.Drawing.Point(264, 72);
this.txtPws.Name = "txtPws";
this.txtPws.PasswordChar = '*';
this.txtPws.Size = new System.Drawing.Size(80, 21);
this.txtPws.TabIndex = 8;
this.txtPws.Text = "";
//
// txtUser
//
this.txtUser.Location = new System.Drawing.Point(72, 72);
this.txtUser.Name = "txtUser";
this.txtUser.Size = new System.Drawing.Size(136, 21);
this.txtUser.TabIndex = 7;
this.txtUser.Text = "";
//
// txtFTP
//
this.txtFTP.Location = new System.Drawing.Point(80, 16);
this.txtFTP.Name = "txtFTP";
this.txtFTP.Size = new System.Drawing.Size(128, 21);
this.txtFTP.TabIndex = 6;
this.txtFTP.Text = "";
//
// btnLiuLan
//
this.btnLiuLan.Location = new System.Drawing.Point(352, 128);
this.btnLiuLan.Name = "btnLiuLan";
this.btnLiuLan.TabIndex = 12;
this.btnLiuLan.Text = "浏览...";
this.btnLiuLan.Click += new System.EventHandler(this.btnLiuLan_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 13;
this.label2.Text = "服务器地址";
//
// label3
//
this.label3.Location = new System.Drawing.Point(224, 16);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 14;
this.label3.Text = "服务器端口";
this.label3.Click += new System.EventHandler(this.label3_Click);
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(296, 16);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(48, 21);
this.txtPort.TabIndex = 15;
this.txtPort.Text = "21";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(48, 23);
this.label4.TabIndex = 16;
this.label4.Text = "用户名:";
//
// label5
//
this.label5.Location = new System.Drawing.Point(216, 72);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(48, 23);
this.label5.TabIndex = 17;
this.label5.Text = "口令:";
//
// label6
//
this.label6.Location = new System.Drawing.Point(24, 104);
this.label6.Name = "label6";
this.label6.TabIndex = 18;
this.label6.Text = "要上传的文件";
//
// label7
//
this.label7.Location = new System.Drawing.Point(80, 40);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(240, 23);
this.label7.TabIndex = 19;
this.label7.Text = "例如:192.168.0.1 或 ftp.xjftp.com";
//
// btnDel
//
this.btnDel.ForeColor = System.Drawing.Color.Black;
this.btnDel.Location = new System.Drawing.Point(24, 208);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(96, 23);
this.btnDel.TabIndex = 20;
this.btnDel.Text = "删除相同文件";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(352, 208);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 21;
this.btnClose.Text = "关闭";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// lstInfs
//
this.lstInfs.ItemHeight = 12;
this.lstInfs.Location = new System.Drawing.Point(16, 240);
this.lstInfs.Name = "lstInfs";
this.lstInfs.Size = new System.Drawing.Size(416, 112);
this.lstInfs.TabIndex = 22;
this.lstInfs.SelectedIndexChanged += new System.EventHandler(this.lstInfs_SelectedIndexChanged);
//
// btnLogin
//
this.btnLogin.Location = new System.Drawing.Point(352, 72);
this.btnLogin.Name = "btnLogin";
this.btnLogin.TabIndex = 23;
this.btnLogin.Text = "登录";
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 152);
this.label1.Name = "label1";
this.label1.TabIndex = 24;
this.label1.Text = "服务器路径:";
//
// txtSvrPath
//
this.txtSvrPath.Location = new System.Drawing.Point(24, 168);
this.txtSvrPath.Name = "txtSvrPath";
this.txtSvrPath.Size = new System.Drawing.Size(304, 21);
this.txtSvrPath.TabIndex = 25;
this.txtSvrPath.Text = "/";
this.txtSvrPath.TextChanged += new System.EventHandler(this.txtSvrPath_TextChanged);
//
// dlgOpen
//
this.dlgOpen.Filter = "所有文件(*.*)|*.*";
this.dlgOpen.Title = "打开文件";
//
// FrmFtp
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(466, 408);
this.Controls.Add(this.txtSvrPath);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.txtFile);
this.Controls.Add(this.txtPws);
this.Controls.Add(this.txtUser);
this.Controls.Add(this.txtFTP);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.lstInfs);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnDel);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnLiuLan);
this.Controls.Add(this.lblInf);
this.Controls.Add(this.btnUpLoad);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "FrmFtp";
this.Text = "文件上传";
this.Controls.SetChildIndex(this.btnUpLoad, 0);
this.Controls.SetChildIndex(this.lblInf, 0);
this.Controls.SetChildIndex(this.btnLiuLan, 0);
this.Controls.SetChildIndex(this.label2, 0);
this.Controls.SetChildIndex(this.label3, 0);
this.Controls.SetChildIndex(this.label4, 0);
this.Controls.SetChildIndex(this.label5, 0);
this.Controls.SetChildIndex(this.label6, 0);
this.Controls.SetChildIndex(this.label7, 0);
this.Controls.SetChildIndex(this.btnDel, 0);
this.Controls.SetChildIndex(this.btnClose, 0);
this.Controls.SetChildIndex(this.lstInfs, 0);
this.Controls.SetChildIndex(this.btnLogin, 0);
this.Controls.SetChildIndex(this.label1, 0);
this.Controls.SetChildIndex(this.txtFTP, 0);
this.Controls.SetChildIndex(this.txtUser, 0);
this.Controls.SetChildIndex(this.txtPws, 0);
this.Controls.SetChildIndex(this.txtFile, 0);
this.Controls.SetChildIndex(this.txtPort, 0);
this.Controls.SetChildIndex(this.txtSvrPath, 0);
this.ResumeLayout(false);
}
#endregion
#endregion
private void label1_Click(object sender, System.EventArgs e)
{
}
{
}
private void btnUpLoad_Click(object sender, System.EventArgs e)
{
if (ftp.IsLogined==false)
{
MessageBox.Show("您没有登录到服务器!请先登录!");
return ;
}
if (ftp.UploadFile(this.txtFile.Text ,this.txtSvrPath.Text ) ==true)
{
MessageBox.Show(ftp.NowInf );//成功
}
else
{
MessageBox.Show(ftp.NowInf);//失败
}
}
{
if (ftp.IsLogined==false)
{
MessageBox.Show("您没有登录到服务器!请先登录!");
return ;
}
if (ftp.UploadFile(this.txtFile.Text ,this.txtSvrPath.Text ) ==true)
{
MessageBox.Show(ftp.NowInf );//成功
}
else
{
MessageBox.Show(ftp.NowInf);//失败
}
}
private void label3_Click(object sender, System.EventArgs e)
{
}
{
}
private void btnLogin_Click(object sender, System.EventArgs e)
{
ftp.Server=this.txtFTP.Text ;
ftp.User=this.txtUser.Text ;
ftp.Password=this.txtPws.Text ;
ftp.Port=this.txtPort.Text ;
ftp.InfsShow=this.lblInf;
ftp.InfsList=this.lstInfs ;
if (ftp.IsLogined==false)
{
ftp.Login();
}
}
{
ftp.Server=this.txtFTP.Text ;
ftp.User=this.txtUser.Text ;
ftp.Password=this.txtPws.Text ;
ftp.Port=this.txtPort.Text ;
ftp.InfsShow=this.lblInf;
ftp.InfsList=this.lstInfs ;
if (ftp.IsLogined==false)
{
ftp.Login();
}
}
private void btnDel_Click(object sender, System.EventArgs e)
{
if (ftp.IsLogined==true)
{
string[] pt=this.txtFile.Text.Split("//".ToCharArray());
string filename=pt[pt.Length-1] ;
ftp.Delete(filename);
MessageBox.Show("重复文件已删除!");
}
else
{
MessageBox.Show("您没有登录到服务器!");
}
{
if (ftp.IsLogined==true)
{
string[] pt=this.txtFile.Text.Split("//".ToCharArray());
string filename=pt[pt.Length-1] ;
ftp.Delete(filename);
MessageBox.Show("重复文件已删除!");
}
else
{
MessageBox.Show("您没有登录到服务器!");
}
}
private void btnClose_Click(object sender, System.EventArgs e)
{
ftp.Logout();
ftp=null;
this.Close();
{
ftp.Logout();
ftp=null;
this.Close();
}
private void txtSvrPath_TextChanged(object sender, System.EventArgs e)
{
if (ftp.IsLogined==true)
{
ftp.SetCurDir(this.txtSvrPath.Text);
}
}
{
if (ftp.IsLogined==true)
{
ftp.SetCurDir(this.txtSvrPath.Text);
}
}
private void btnLiuLan_Click(object sender, System.EventArgs e)
{
{
this.dlgOpen.FileName=this.txtFile.Text ;
if (this.dlgOpen.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
this.txtFile.Text=this.dlgOpen.FileName;
}
}
if (this.dlgOpen.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
this.txtFile.Text=this.dlgOpen.FileName;
}
}
private void lstInfs_SelectedIndexChanged(object sender, System.EventArgs e)
{
{
}
}
}
}
}