日期:2014-05-19  浏览次数:20904 次

如何用C#获取FTP服务器文件列表?
请高手指点,谢谢!

------解决方案--------------------
vs2005有一个专门的ftp类,你仔细看看,里面应该有方法
------解决方案--------------------
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;

namespace zhangyuk.net.csdn.blog.ftpclient
{
/// <summary>
/// FTP Client
/// </summary>
public class FTPClient
{
#region 构造函数
/// <summary>
/// 缺省构造函数
/// </summary>
public FTPClient()
{
strRemoteHost = " ";
strRemotePath = " ";
strRemoteUser = " ";
strRemotePass = " ";
strRemotePort = 21;
bConnected = false;
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name= "remoteHost "> </param>
/// <param name= "remotePath "> </param>
/// <param name= "remoteUser "> </param>
/// <param name= "remotePass "> </param>
/// <param name= "remotePort "> </param>
public FTPClient( string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort )
{
strRemoteHost = remoteHost;
strRemotePath = remotePath;
strRemoteUser = remoteUser;
strRemotePass = remotePass;
strRemotePort = remotePort;
Connect();
}
#endregion

#region 登陆
/// <summary>
/// FTP服务器IP地址
/// </summary>
private string strRemoteHost;
public string RemoteHost
{
get
{
return strRemoteHost;
}
set
{
strRemoteHost = value;
}
}
/// <summary>
/// FTP服务器端口
/// </summary>
private int strRemotePort;
public int RemotePort
{
get
{
return strRemotePort;
}
set
{
strRemotePort = value;
}
}
/// <summary>
/// 当前服务器目录
/// </summary>
private string strRemotePath;
public string RemotePath
{
get
{
return strRemotePath;
}
set
{
strRemotePath = value;
}
}
/// <summary>
/// 登录用户账号
/// </summary>
private string strRemoteUser;
public string RemoteUser
{
set
{
strRemoteUser = value;
}
}
/// <summary>
/// 用户登录密码
/// </summary>
private string strRemotePass;
public string RemotePass
{
set
{
strRemotePass = value;
}
}

/// <summary>
/// 是否登录
/// </summary>
private Boolean bConnected;
public bool Connected
{
get
{
return bConnected;
}
}
#endregion


------解决方案--------------------
#region 链接
/// <summary>
/// 建立连接
/// </summary>
public void Connect()
{
socketControl = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);
// 链接
try
{
socketControl.Connect(ep);