各位好~遇上了个问题在一个方法中建立了FtpWebRequest对象,已经指向FTP的根目录了,我读取完我需要的数据(读取所有的文件名)后,想要跳到根目录下的每个目录,再次读取每个目录下的文件名,这时,报的错误信息是“请求的URI对此FTP命令无效”,请问是什么原因?谢谢了~~我在猜测是不是由于每次建立的FTP连接只能是一个?
public static string[] FTPGetFileList(string ftpServerIP, string ftpUserID, string ftpPassword)
{
//响应结果
StringBuilder result = new StringBuilder(); //FTP请求
FtpWebRequest ftpRequest = null; //FTP响应
WebResponse ftpResponse = null; //FTP响应流
StreamReader ftpResponsStream = null;
try
{
//生成FTP请求
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
//设置文件传输类型
ftpRequest.UseBinary = true; //FTP登录
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); //设置FTP方法
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; //生成FTP响应
ftpResponse = ftpRequest.GetResponse(); //FTP响应流
ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream(), System.Text.Encoding.Default);
Console.WriteLine(ftpResponsStream.CurrentEncoding); string line = ftpResponsStream.ReadLine(); while (line != null)
{
if (line.IndexOf("<DIR>") != -1)
{
Console.WriteLine(line.Substring(39));//此处获得目录名,测试用
result.Append(GetSubFilesList(ftpServerIP,ftpUserID,ftpPassword,line.Substring(39)));
}
else
{
result.Append(line);
result.Append("\n");
} line = ftpResponsStream.ReadLine();
} //去掉结果列表中最后一个换行
result.Remove(result.ToString().LastIndexOf('\n'), 1); //返回结果
return result.ToString().Split('\n');
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return (null);
}
finally
{
if (ftpResponsStream != null)
{
ftpResponsStream.Close();
} if (ftpResponse != null)
{
ftpResponse.Close();
}
}
}private static string GetSubFilesList(string ftpServerIP,string ftpUserID, string ftpPassword, string StrDir)
{
//响应结果
StringBuilder result = new StringBuilder();
string struri = "ftp://" + ftpServerIP + "/" + StrDir + "/"; //FTP请求
FtpWebRequest ftpRequest = null; //FTP响应
WebResponse ftpResponse = null; //FTP响应流
StreamReader ftpResponsStream = null; try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(struri)); //生成FTP响应
ftpResponse = ftpRequest.GetResponse(); //FTP响应流
ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream(), System.Text.Encoding.Default);
Console.WriteLine(ftpResponsStream.CurrentEncoding); string line = ftpResponsStream.ReadLine(); while (line != null)
{
if (line.IndexOf("<DIR>") != -1)
{
Console.WriteLine(line.Substring(39));
result.Append(GetSubFilesList(ftpServerIP,ftpUserID,ftpPassword,line.Substring(39)));//此处希望通过递归调用获取子目录下的文件名
}
else
{
result.Append(line);
result.Append("\n");
} line = ftpResponsStream.ReadLine();
} return result.ToString();
}
...
}
public static string[] FTPGetFileList(string ftpServerIP, string ftpUserID, string ftpPassword)
{
//响应结果
StringBuilder result = new StringBuilder(); //FTP请求
FtpWebRequest ftpRequest = null; //FTP响应
WebResponse ftpResponse = null; //FTP响应流
StreamReader ftpResponsStream = null;
try
{
//生成FTP请求
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
//设置文件传输类型
ftpRequest.UseBinary = true; //FTP登录
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); //设置FTP方法
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; //生成FTP响应
ftpResponse = ftpRequest.GetResponse(); //FTP响应流
ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream(), System.Text.Encoding.Default);
Console.WriteLine(ftpResponsStream.CurrentEncoding); string line = ftpResponsStream.ReadLine(); while (line != null)
{
if (line.IndexOf("<DIR>") != -1)
{
Console.WriteLine(line.Substring(39));//此处获得目录名,测试用
result.Append(GetSubFilesList(ftpServerIP,ftpUserID,ftpPassword,line.Substring(39)));
}
else
{
result.Append(line);
result.Append("\n");
} line = ftpResponsStream.ReadLine();
} //去掉结果列表中最后一个换行
result.Remove(result.ToString().LastIndexOf('\n'), 1); //返回结果
return result.ToString().Split('\n');
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return (null);
}
finally
{
if (ftpResponsStream != null)
{
ftpResponsStream.Close();
} if (ftpResponse != null)
{
ftpResponse.Close();
}
}
}private static string GetSubFilesList(string ftpServerIP,string ftpUserID, string ftpPassword, string StrDir)
{
//响应结果
StringBuilder result = new StringBuilder();
string struri = "ftp://" + ftpServerIP + "/" + StrDir + "/"; //FTP请求
FtpWebRequest ftpRequest = null; //FTP响应
WebResponse ftpResponse = null; //FTP响应流
StreamReader ftpResponsStream = null; try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(struri)); //生成FTP响应
ftpResponse = ftpRequest.GetResponse(); //FTP响应流
ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream(), System.Text.Encoding.Default);
Console.WriteLine(ftpResponsStream.CurrentEncoding); string line = ftpResponsStream.ReadLine(); while (line != null)
{
if (line.IndexOf("<DIR>") != -1)
{
Console.WriteLine(line.Substring(39));
result.Append(GetSubFilesList(ftpServerIP,ftpUserID,ftpPassword,line.Substring(39)));//此处希望通过递归调用获取子目录下的文件名
}
else
{
result.Append(line);
result.Append("\n");
} line = ftpResponsStream.ReadLine();
} return result.ToString();
}
...
}
解决方案 »
- 【oj每周推荐】谈谈C#中的泛型
- c# winform 自定义Image属性
- 如何用base64加密.zip文件,生成一串字符串?
- C#如何向别的程序里发送模拟按键或鼠标操作?
- 初学者
- 电子商务的在线支付问题。
- C#(winform)中如何实现像MSN或QQ那样,弹出某某人刚刚登陆的提示信息框,并且点击信息框中的连接会打开相应的网站
- 存储过程返回2记录集,如何指定一个Fill到单表DataSet
- 如何获得一台机上的网卡数量???
- winform求助,计时器滚动5秒(上限引用之前的全局变量)然后停止,再显示之前的结果变量(多个,每次一个不重复)停留2秒,重复运行,到1分钟后自动结束
- MDI,打开两个子窗口,最小化,接着在最小化状态下关闭,然后父窗口不能关闭
- C# 写入数据库 有时候写不进去
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
检查服务器
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);