花了几个小时终于把ftp对象整理好了!!!
C# code
private
string strHost, strPath, strUser, strPassword, strMessage; private
int intPort, bytes; private Socket socket; private
int intStatusNumber; private
string strReply; Byte[] buffer =
new Byte[1024]; Encoding encoding = Encoding.Default; #region 基本构造函数
///
<summary>
/// 构造函数(默认端口21) ///
</summary>
///
<param name="strHost">主机IP</param>
///
<param name="strUser">账号</param>
///
<param name="strPassword">密码</param>
public FTP(string strHost, string strUser, string strPassword) : this(strHost, 21, strUser, strPassword) { } ///
<summary>
/// 构造函数 ///
</summary>
///
<param name="strHost">主机IP</param>
///
<param name="intPort">端口号</param>
///
<param name="strUser">账号</param>
///
<param name="strPassword">密码</param>
public FTP(string strHost, int intPort, string strUser, string strPassword) { this.strHost = strHost; this.strPath =
"."; this.strUser = strUser; this.strPassword = strPassword; this.intPort = intPort; } public
void SetEncoding(Encoding encoding) { this.encoding = encoding; } public
void SetEncoding(string strName) { this.encoding = Encoding.GetEncoding(strName); } public
void SetEncoding(int encodingIndex) { this.encoding = Encoding.GetEncoding(encodingIndex); } public
void Setbuffer(uint buff) { this.buffer =
new
byte[buff]; } #endregion
///
<summary>
/// 得到一个目录下的List ///
</summary>
///
<param name="strName">目录名</param>
///
<returns></returns>
public
string[] GetFileList(string strName) { Socket sk = CreateSocket(); SetCommand("NLST "
+ strName); if (!(intStatusNumber ==
150
|| intStatusNumber ==
125)) { return
null; } strMessage =
string.Empty; while (true) { int bytes = sk.Receive(buffer, buffer.Length, 0); strMessage += encoding.GetString(buffer, 0, bytes); if (bytes < buffer.Length) { break; } } string[] strFileNames = strMessage.Split(new
char[] { '\n' }); sk.Close(); ChangeValue(); if (intStatusNumber !=
226) { throw
new IOException(strReply.Substring(4)); } return strFileNames; } ///
<summary>
/// 得到文件大小 ///
</summary>
///
<param name="fileName">文件路径</param>
///
<returns>-1表示出现错误</returns>
public
int GetFileSize(string fileName) { try { SetCommand("SIZE "
+ fileName); int size =
0; if (intStatusNumber ==
213) { size =
int.Parse(strReply.Substring(4)); } else { throw
new IOException(strReply.Substring(4)); } return size; } catch { return
-1; } } ///
<summary>
/// 登陆到FTP服务器 ///
</summary>
public
bool Login() { bool blLogin =
false; socket =
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep =
new IPEndPoint(IPAddress.Parse(strHost), intPort); try { socket.Connect(ep); ChangeValue(); if (intStatusNumber !=
220) { Close(); throw
new IOException(strReply.Substring(4)); } SetCommand("USER "
+ strUser); if (!(intStatusNumber ==
331
|| intStatusNumber ==
230)) { CleanUP(); throw
new IOException(strReply.Substring(4)); } if (intStatusNumber !=
230) { SetCommand("PASS "
+ strPassword); if (!(intStatusNumber ==
230
|| intStatusNumber ==
202)) { CleanUP(); throw
new IOException(strReply.Substring(4)); } } blLogin =
true; } catch (Exception) { throw
new IOException("Couldn't connect to remote server"); } return blLogin; } ///
<summary>
/// 下载文件 ///
</summary>
///
<param name="strRemoteName">远程地址</param>
///
<param name="strLocalName">保存本地路径</param>
public
bool DownLoadFile(string strRemoteName, string strLocalName) { bool bl =
true; FileStream stream =
null; Socket sk =
null; try { if (!File.Exists(strLocalName)) { Stream st = File.Create(strLocalName); st.Close(); } stream =
new FileStream(strLocalName, FileMode.Open); sk = CreateSocket(); SetCommand("RETR "
+ strRemoteName); if (!(intStatusNumber ==
150
|| intStatusNumber ==
125)) { throw
new IOException(strReply.Substring(4)); } while (true) { bytes = sk.Receive(buffer, buffer.Length, 0); stream.Write(buffer, 0, bytes); if (bytes <=
0) { break; } } ChangeValue(); if (!(intStatusNumber ==
226
|| intStatusNumber ==
250)) { throw
new IOException(strReply.Substring(4)); } } catch { bl =
false; } finally { if (sk !=
null) { sk.Close(); } if (stream !=
null) { stream.Close(); } } return bl; } ///
<summary>
/// 上传文件 ///
</summary>
///
<param name="fileName">文件名</param>
///
<param name="strNewName">新的文件名如 abc /// 如果为空那么采用原来的文件名</param>
public
bool UPLoadFile(string fileName,string strNewName) { bool bl =
true; Socket sk = CreateSocket(); FileStream stream =
null; try { if (strNewName.Length ==
0) { SetCommand("STOR "
+ Path.GetFileName(fileName)); } else { SetCommand("STOR "
+ strNewName + fileName.Substring(fileName.LastIndexOf("."))); } if (!(intStatusNumber ==
125
|| intStatusNumber ==
150)) { throw
new IOException(strReply.Substring(4)); } stream =
new FileStream(fileName, FileMode.Open); while ((bytes = stream.Read(buffer, 0, buffer.Length)) >
0) { sk.Send(buffer, bytes, 0); } ChangeValue(); if (!(intStatusNumber ==
226
|| intStatusNumber ==
250)) { throw
new IOException(strReply.Substring(4)); } } catch { bl =
false; } finally { if (sk !=
null) { sk.Close(); } if (stream !=
null) { stream.Close(); } } return bl; }
[ 本帖最后由 兔子的军团 于 2008-6-12 11:05 编辑 ]
|