ftp问题,各位帮下忙
用过ftp的人应该有好多人看过这个例子吧:
http://www.codeproject.com/csharp/ftplibrary.asp
我使用public string[] GetFileList(string mask)方法获取ftp服务器上文件列表,参数File为文件名或者文件夹,当File不存在时,出现了问题
1。局域网ftp服务器,设置了ftp,匿名登陆,默认用户为administrator
this.resultCode == 125
所以可以通过该方法查找某个File是否存在
2。远程ftp服务器,用户具有增,删,改权限
this.resultCode = 550;
所以在这一句
if(!(this.resultCode == 150 || this.resultCode == 125)) throw new FtpException(this.result.Substring(4));
就会跳出,提示文件或者目录不存在的错误
why???!!!
public string[] GetFileList(string File)
{
if ( !this.loggedin ) this.Login();
Socket cSocket = createDataSocket();
this.sendCommand("NLST -U " + File);
if(!(this.resultCode == 150 || this.resultCode == 125)) throw new FtpException(this.result.Substring(4));
this.message = "";
DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);
while( timeout > DateTime.Now )
{
int bytes = cSocket.Receive(buffer, buffer.Length, 0);
this.message += ASCII.GetString(buffer, 0, bytes);
if ( bytes < this.buffer.Length ) break;
}
string[] msg = this.message.Replace("\r","").Split('\n');
cSocket.Close();
if ( this.message.IndexOf( "No such file or directory" ) != -1 )
msg = new string[]{};
this.readResponse();
if ( this.resultCode != 226 )
msg = new string[]{};
// throw new FtpException(result.Substring(4));
return msg;
}
问题点数:100、回复次数:1Top
1 楼vivianfdlpw()回复于 2006-03-14 16:28:44 得分 100
/// <summary>
/// 获取指定模式的文件列表
/// </summary>
/// <param name="mask"></param>
/// <returns></returns>
public virtual string[] GetFileList(string mask)
{
if (!this.loggedin) this.Login();
System.Net.Sockets.Socket cSocket = createDataSocket();
this.SendCommand("LIST " + mask);
if (!(this.resultCode == 150 || this.resultCode == 125 || this.resultCode == 250 || this.resultCode == 226)) throw new FtpServerNotAvailableException(this.result.Substring(4));
this.message = "";
DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);
while (timeout > DateTime.Now)
{
int bytes = cSocket.Receive(buffer, buffer.Length, 0);
this.message += ASCII.GetString(buffer, 0, bytes);
if (bytes < this.buffer.Length) break;
}
string[] msg = this.message.Replace("\r", "").Split('\n');
cSocket.Close();
if (this.message.IndexOf("No such file or directory") != -1)
msg = new string[] { };
return msg;
}
Top




