各位好~遇上了个问题在一个方法中建立了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();
            }
            ...
        }

解决方案 »

  1.   

    下面这句的uri你指写了ip是不够的,还要加上具体的文件或者目录//生成FTP请求
    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
      

  2.   

    是第二个方法里的吗?我有把具体的目录名加上去啊~string struri = "ftp://" + ftpServerIP + "/" + StrDir + "/";
      

  3.   

    Create( "ftp://"+ftpServerIP +"/a.txt ")
    检查服务器
    FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);   
      

  4.   

    MSDN上有类似问题。注意ftp://192.168.1.1/path与ftp://192.168.1.1//path的区别。后一种才是完美的