|
FTPClient--FTP操作帮助类,上传下载,文件,目录操作
导读部分
C#基类库苏飞版--系列教程导航 http://www.cckan.net/thread-655-1-1.html
这个类是关于加密,解密的操作,文件的一些高级操作
1.构造函数
2.字段 服务器账户密码
3.属性
4.链接
5.传输模式
6.文件操作
7.上传和下载
8.目录操作
9.内容函数
看下面代码吧
///
/// 类说明:CacheHelper
/// 联系方式:361983679
/// 更新网站:http://www.cckan.net/thread-655-1-1.html
///
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace DotNet.Utilities
{
///
/// FTP 操作类客户端
///
public class FTPClient
{
public static object obj = new object();
#region 构造函数
///
/// 缺省构造函数
///
public FTPClient()
{
strRemoteHost = "";
strRemotePath = "";
strRemoteUser = "";
strRemotePass = "";
strRemotePort = 21;
bConnected = false;
}
///
/// 构造函数
///
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 字段
private int strRemotePort;
private Boolean bConnected;
private string strRemoteHost;
private string strRemotePass;
private string strRemoteUser;
private string strRemotePath;
///
/// 服务器返回的应答信息(包含应答码)
///
private string strMsg;
///
/// 服务器返回的应答信息(包含应答码)
///
private string strReply;
///
/// 服务器返回的应答码
///
private int iReplyCode;
///
/// 进行控制连接的socket
///
private Socket socketControl;
///
/// 传输模式
///
private TransferType trType;
///
/// 接收和发送数据的缓冲区
///
private static int BLOCK_SIZE = 512;
///
/// 编码方式
///
Encoding ASCII = Encoding.ASCII;
///
/// 字节数组
///
Byte[] buffer = new Byte[BLOCK_SIZE];
#endregion
#region 属性
///
/// FTP服务器IP地址
///
public string RemoteHost
{
get
{
return strRemoteHost;
}
set
{
strRemoteHost = value;
}
}
///
/// FTP服务器端口
///
public int RemotePort
{
get
{
return strRemotePort;
}
set
{
strRemotePort = value;
}
}
///
/// 当前服务器目录
///
public string RemotePath
{
get
{
return strRemotePath;
}
set
{
strRemotePath = value;
}
}
///
/// 登录用户账号
///
public string RemoteUser
{
set
{
strRemoteUser = value;
}
}
///
/// 用户登录密码
///
public string RemotePass
{
set
{
strRemotePass = value;
}
}
///
/// 是否登录
///
public bool Connected
{
get
{
return bConnected;
}
}
#endregion
#region 链接
///
/// 建立连接
///
public void Connect()
{
lock (obj)
{
socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);
try
{
socketControl.Connect(ep);
}
catch (Exception)
{
throw new IOException("不能连接ftp服务器");
}
}
ReadReply();
if (iReplyCode != 220)
{
DisConnect();
throw new IOException(strReply.Substring(4));
}
SendCommand("USER " + strRemoteUser);
if (!(iReplyCode == 331 || iReplyCode == 230))
{
CloseSocketConnect();
throw new IOException(strReply.Substring(4));
}
if (iReplyCode != 230)
{
SendCommand("PASS " + strRemotePass);
if (!(iReplyCode == 230 || iReplyCode == 202))
{
CloseSocketConnect();
throw new IOException(strReply.Substring(4));
}
}
bConnected = true;
ChDir(strRemotePath);
}
///
/// 关闭连接
///
public void DisConnect()
{
if (socketControl != null)
{
SendCommand("QUIT");
}
CloseSocketConnect();
}
#endregion
#region 传输模式
///
/// 传输模式:二进制类型、ASCII类型
///
public enum TransferType { Binary, ASCII };
///
/// 设置传输模式
///
/// 传输模式
public void SetTransferType(TransferType ttType)
{
if (ttType == TransferType.Binary)
{
SendCommand("TYPE I");//binary类型传输
}
else
{
SendCommand("TYPE A");//ASCII类型传输
}
if (iReplyCode != 200)
{
throw new IOException(strReply.Substring(4));
}
else
{
trType = ttType;
}
}
///
/// 获得传输模式
///
/// 传输模式
public TransferType GetTransferType()
{
return trType;
}
#endregion
#region 文件操作
///
/// 获得文件列表
///
/// 文件名的匹配字符串
public string[] Dir(string strMask)
{
if (!bConnected)
{
Connect();
}
Socket socketData = CreateDataSocket();
SendCommand("NLST " + strMask);
if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226))
{
throw new IOException(strReply.Substring(4));
}
strMsg = "";
Thread.Sleep(2000);
while (true)
{
int iBytes = socketData.Receive(buffer, buffer.Length, 0);
strMsg += ASCII.GetString(buffer, 0, iBytes);
if (iBytes < buffer.Length)
{
break;
}
}
char[] seperator = { '\n' };
string[] strsFileList = strMsg.Split(seperator);
socketData.Close(); //数据socket关闭时也会有返回码
if (iReplyCode != 226)
{
ReadReply();
if (iReplyCode != 226)
{
throw new IOException(strReply.Substring(4));
}
}
return strsFileList;
}
public void newPutByGuid(string strFileName, string strGuid)
{
if (!bConnected)
{
Connect();
}
string str = strFileName.Substring(0, strFileName.LastIndexOf("\\"));
string strTypeName = strFileName.Substring(strFileName.LastIndexOf("."));
strGuid = str + "\\" + strGuid;
Socket socketData = CreateDataSocket();
SendCommand("STOR " + Path.GetFileName(strGuid));
if (!(iReplyCode == 125 || iReplyCode == 150))
{
throw new IOException(strReply.Substring(4));
}
FileStream input = new FileStream(strGuid, FileMode.Open);
input.Flush();
int iBytes = 0;
while ((iBytes = input.Read(buffer, 0, buffer.Length)) > 0)
{
socketData.Send(buffer, iBytes, 0);
}
input.Close();
if (socketData.Connected)
{
socketData.Close();
}
if (!(iReplyCode == 226 || iReplyCode == 250))
{
ReadReply();
if (!(iReplyCode == 226 || iReplyCode == 250))
{
throw new IOException(strReply.Substring(4));
}
}
}
///
/// 获取文件大小
///
/// 文件名
/// 文件大小
public long GetFileSize(string strFileName)
{
if (!bConnected)
{
Connect();
}
SendCommand("SIZE " + Path.GetFileName(strFileName));
long lSize = 0;
if (iReplyCode == 213)
{
lSize = Int64.Parse(strReply.Substring(4));
}
else
{
throw new IOException(strReply.Substring(4));
}
return lSize;
}
///
/// 获取文件信息
///
/// 文件名
/// 文件大小
public string GetFileInfo(string strFileName)
{
if (!bConnected)
{
Connect();
}
Socket socketData = CreateDataSocket();
SendCommand("LIST " + strFileName);
string strResult = "";
if (!(iReplyCode == 150 || iReplyCode == 125
|| iReplyCode == 226 || iReplyCode == 250))
{
throw new IOException(strReply.Substring(4));
}
byte[] b = new byte[512];
MemoryStream ms = new MemoryStream();
while (true)
{
int iBytes = socketData.Receive(b, b.Length, 0);
ms.Write(b, 0, iBytes);
if (iBytes |
|
|