操作FTP方法:/// 获取文件夹内文件信息
/// </summary>
/// <param name="BaseUriStr">基Uri</param>
/// <param name="AimUriStr">目标目录,相对路径</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static List<string> ListFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
        {
            Uri BaseUri = new Uri(BaseUriStr);
            Uri AimUri = new Uri(BaseUri, AimUriStr);
            FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
            //FtpRequest.KeepAlive = true;
            FtpRequest.KeepAlive = false;
            FtpRequest.ReadWriteTimeout = 10000;
            FtpRequest.UsePassive = false;
            FtpRequest.Proxy = null;
            NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
            CredentialCache FtpCache = new CredentialCache();
            FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
            FtpRequest.Credentials = FtpCache;
            FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;                        try
            {
                FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
                StreamReader srd = new StreamReader(FtpResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
                string ResponseBackStr = srd.ReadToEnd();
                srd.Close();
                FtpResponse.Close();                string[] ListDetails = ResponseBackStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                List<string> RtnList = new List<string>();
                foreach (string ListDetail in ListDetails)
                {
                    if (ListDetail.StartsWith("d") && (!ListDetail.EndsWith(".")))
                    {
                        string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
                        RtnList.Add(FtpDirName + "|D");
                    }
                    else if (ListDetail.StartsWith("-"))
                    {
                        string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
                        RtnList.Add(FtpDirName + "|F");
                    }
                }                return RtnList;
            }
            catch (WebException e)
            {
                FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
                FtpResponse.Close();                return new List<string>();
            }
        }
返加一个list,保存该文件夹内所有文件名。循环执行这个程序,用于返回所有文件夹的所有文件文件名,循环第一遍没问题,第二遍时,获取另一个文件夹时,报错:
远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)文件夹是存在的,因为就算执行2次这个信函数,参数不变,执行到第2次的时候也会报这个错误 ,不知道是什么原因引起的。谢谢大家帮忙 。

解决方案 »

  1.   

    看这里:
    C#封装的完整FTP类
      

  2.   

    问题原因找到了,但是还是不知道怎样解决我在调用ListFtpDirectory()之后,使用了Webclient下载文件,然后再使用ListFtpDirectory()就报错,不使用Webclient下载文件则不会。
    public void DownLoadFile(string localFilePath, string serverFolder)
            {
                string uriString;            uriString = serverFolder;   // 服务器保存路径
                /**/
                /// 创建WebClient实例
                WebClient myWebClient = new WebClient();
                Uri uUri = new Uri(uriString);
                // WEB 身份验证
                NetworkCredential WebCred = new NetworkCredential("name", "pwd");
                CredentialCache WebCache = new CredentialCache();
                WebCache.Add(uUri, AuthType.Basic.ToString(), WebCred);
                myWebClient.Credentials = WebCache;
                myWebClient.Proxy = null;
                try
                {
                      myWebClient.DownloadFile (uUri, localFilePath);                
                    myWebClient.Dispose();
                }
                catch (Exception err)
                {
                }        }实在不知道该怎样解决了....每当调用上面方法从FTP下载文件后,再执行1楼的方法,就会报错。
      

  3.   

    估计是WebClient 下载完文件之后没有断开链接,但是没有找到断开的方法。
      

  4.   

    fire.exists("路径")判断一下文件是否存在
      

  5.   


    文件是存在的,这点可以肯定,ListFtpDirectory(....) 函数参数不变,执行第一次没问题,第二次就报550。在此期间执行了FTP的文件下载操作,就会出现这个问题。
      

  6.   


    其实有点不好意思说,问题并没有真正解决,只是将WEBCLIEN下载文件,改用了相同的FtpWebRequest。
    注意KeepAlive = false;否则同样会报550
      

  7.   

    我也很希望能够有一种方法实现WEBCLIEN与FtpWebRequest同时对FTP进行操作。
      

  8.   

    FtpRequest.KeepAlive = false;
    我设置了还是会报错啊,我用的是递归遍历整个FTP取得所有文件.文件少的时候不出错,多了就出550错误.
      

  9.   

    使用WebRequestMethods.Ftp.ListDirectoryDetails时
    即使设置了 request.KeepAlive = false;
    收到LIST命令的Response数据,客户端没有发QUIT命令来结束FTP连接。
    再次运行时就失败了。不知怎么解决
      

  10.   

    我的是第一次下载可以,第二次就不可以了为什么?
     public  bool Download(string filePath, string fileName, out string errorinfo)/**/////上面的代码实现了从ftp服务器下载文件的功能
            {            try
                {
                    string onlyFileName = Path.GetFileName(fileName);
                    //string newFileName = filePath + "\\" + onlyFileName;
                    string newFileName = "E:\\VMI\\" + onlyFileName;
                    if (File.Exists(newFileName))
                    {
                        errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName);
                        return false;
                    }
                    string url = "ftp://" + ftpServerIP + "/" +filePath+ "/"+fileName;
                    Connect(url);//连接                  reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();                Stream ftpStream = response.GetResponseStream();                long cl = response.ContentLength;                int bufferSize = 2048;                int readCount;                byte[] buffer = new byte[bufferSize];                readCount = ftpStream.Read(buffer, 0, bufferSize);
                    FileStream outputStream = new FileStream(newFileName, FileMode.Create);                while (readCount > 0)
                    {
                        outputStream.Write(buffer, 0, readCount);                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    }                ftpStream.Close();                outputStream.Close();
                    response.Close();
                    errorinfo = "";
                    return true;
                }
                catch (Exception ex)
                {
                    errorinfo = string.Format("因{0},无法下载", ex.Message);
                    return false;
                }
            }