首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#实现ftp文件传输源码(winform),分不够再加,该如何解决

2011-12-28 
C#实现ftp文件传输源码(winform),分不够再加我想要一个完整的能调试一步一步看步骤实现功能的例子不用很复

C#实现ftp文件传输源码(winform),分不够再加
我想要一个完整的   能调试一步一步看步骤实现功能的例子
不用很复杂的功能
tcp和ftp   最好都有一份

[解决办法]
using System;
using System.IO;
using System.Net.Sockets;

namespace ftpClientUtil
{
internal class FtpWebStream : Stream
{
private FtpWebResponse response;
private NetworkStream dataStream;

public FtpWebStream(NetworkStream dataStream, FtpWebResponse response)
{
this.dataStream = dataStream;
this.response = response;
}

public override void Close()
{
response.Close();
base.Close();
}

public override void Flush()
{
dataStream.Flush();
}

public override int Read(byte[] buffer, int offset, int count)
{
return dataStream.Read(buffer, offset, count);
}

public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException( "Seek not supported. ");
}

public override void SetLength(long value)
{
throw new NotSupportedException( "SetLength not supported. ");
}

public override void Write(byte[] buffer, int offset, int count)
{
dataStream.Write(buffer, offset, count);
}

public override bool CanRead
{
get { return dataStream.CanRead; }
}

public override bool CanSeek
{
get { return false; }
}

public override bool CanWrite
{
get { return dataStream.CanWrite; }
}

public override long Length
{
get { throw new NotSupportedException( "Length not supported. "); }
}

public override long Position
{
get
{
throw new NotSupportedException( "Position not supported. ");
}
set
{
throw new NotSupportedException( "Position not supported. ");
}
}
}
}

[解决办法]
using System;
using System.Net.Sockets;


/// <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;}
}
int _MsgID;
public int MsgID
{
set{ _MsgID=value;}
get{return _MsgID;}
}
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;}
}
System.Windows.Forms.ProgressBar _pb=new System.Windows.Forms.ProgressBar() ;
public System.Windows.Forms.ProgressBar Progress
{
set{_pb=value;}
get{return _pb;}
}
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( "密码错误! ",230);
return false;
}
SetInf( "用户名错误 ",331);
return false;
}
else
{
SetInf( "服务器没有正确响应! ",220);
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);
if (msg.IndexOf( "\r\n ")> =0)
{
string xx=msg.Substring(msg.IndexOf( "\r\n ")) ;

msg=(xx== "\r\n "?msg:xx);
msg=msg.Replace( "\r\n ", " ");
}
msg+= "\r\n ";
return (msg.Substring(0,wait.Length).ToLower()==wait.ToLower() );//如果msg的前部分是wait,则返回成功.
}
private bool RetOk(string s1,string s2)
{
string msg=s1;
if (msg.IndexOf( "\r\n ")> =0)
{
string xx=msg.Substring(msg.IndexOf( "\r\n ")) ;
msg=(xx== "\r\n "?msg:xx);
msg=msg.Replace( "\r\n ", " ");
}
s1=msg+ "\r\n ";
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);
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();

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)
{
if( msg.IndexOf( "\r\n ")> =0)
{
msg=msg.Substring(0,msg.LastIndexOf( "\r\n ")- "\r\n ".Length );
msg+= " "+ System.DateTime.Now.TimeOfDay.ToString();
}
NowInf=msg;
_infx.Text=NowInf;
_infs.Items.AddRange(msg.Split( "\r\n ".ToCharArray()));
System.Windows.Forms.Application.DoEvents();
_infs.SelectedIndex=_infs.Items.Count-1;
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
}

private void SetInf(string msg,int msgid)
{
SetInf(msg);
_MsgID=msgid;
}

private string GetMsg()
{
string msg;
byte[] data=new byte[1024];
int bytes;
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
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;
bool ok=DoCmd( "QUIT ", "221 ");
SetInf( "已断开服务器! ",221);
return ok;

}
public bool SetCurDir(string Path)
{


if (DoCmd( "CWD "+Path, "250 ")==false)// " " is current directory.
{
SetInf( "确认当前目录失败! ",257);
return false;
}
if (DoCmd( "PWD "+Path, "257 ")==false)// " " is current directory.
{
SetInf( "确认当前目录失败! ",257);
return false;
}
return true;
}
public string GetCurDir()
{


string msg=DoCommand( "PWD ");
string[] st=msg.Split((char)34);
return st[1];
}

public bool Delete(string FileName)
{
return DoCmd( "DELE "+FileName, "250 ");
}

[解决办法]
翻译了一篇“.NET 2.0下简单的FTP访问程序”有源代码

需要的访问:

http://bbs.msproject.cn/default.aspx?g=posts&t=233


热点排行