class RequstFTP
    {
        string ftpServerIP;
        string ftpUserID;
        string ftpPassword;
        FtpWebRequest reqFTP;        public void Connecttest(string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP));
            // 指定数据传输类型
            reqFTP.UseBinary = false;
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        }        #region 连接
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="path"></param>
        private void Connect(String path)//连接ftp
        {
            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
            // 指定数据传输类型
            reqFTP.UseBinary = false;
            //使用主动(port)模式
            reqFTP.UsePassive = false;
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        }
        #endregion        #region ftp登录信息
        /// <summary>
        /// ftp登录信息
        /// </summary>
        /// <param name="ftpServerIP">ftpServerIP</param>
        /// <param name="ftpUserID">ftpUserID</param>
        /// <param name="ftpPassword">ftpPassword</param>
        public void FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            this.ftpServerIP = ftpServerIP;
            this.ftpUserID = ftpUserID;
            this.ftpPassword = ftpPassword;
        }
        #endregion        #region 获取文件列表
        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="path"></param>
        /// <param name="WRMethods"></param>
        /// <returns></returns>
        private string[] GetFileList(string path, string WRMethods)//上面的代码示例了如何从ftp服务器上获得文件列表
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            try
            {
                Connect(path);
                reqFTP.Method = WRMethods;
                WebResponse response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);//中文文件名
                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }
                // to remove the trailing '\n'
                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();
                response.Close();
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                downloadFiles = null;
                return downloadFiles;
            }
        }
        public string[] GetFileList(string path)//上面的代码示例了如何从ftp服务器上获得文件列表
        {
            return GetFileList("ftp://" + ftpServerIP + "//" + path, WebRequestMethods.Ftp.ListDirectory);
        }
        public string[] GetFileList()//上面的代码示例了如何从ftp服务器上获得文件列表
        {
            return GetFileList("ftp://" + ftpServerIP + "//", WebRequestMethods.Ftp.ListDirectory);
        }
        #endregion              
这个是之前自己做的一个测试,获取FTP目录上的文件列表,但是运行到WebResponse response = reqFTP.GetResponse();的时候报‘基础连接已经关闭: 连接被意外关闭’异常。
现在想求一份FTP下载的代码,可以批量下载的。这个下载的模式跟剪切操作一样,如果FTP上的文件正在被使用操作希望会抛个异常出来。。急!!!在线等,SOS.................

解决方案 »

  1.   

    你先确认服务器是否正常,网上例子很多http://www.cnblogs.com/sufei/archive/2011/05/22/2053642.html
      

  2.   

    服务器是正常的,这个代码就是网上的例子之一,不知道会不会是代码写错了  还是传的IP不对。
    还有很重要的一个问题就是C#的FTP下载能不能达到一个剪切的操作,就像我Ctrl+X的操作。
    FTP服务器上的某个文件正在被打开使用(例如正在写入),如果下载到这个文件的话希望它会抛个异常出来。FtpWebRequest提供的下载不知道会不会报错??因为现在连文件名都没获取成功~
      

  3.   

    WebResponse response = reqFTP.GetResponse();的时候报‘基础连接已经关闭: 连接被意外关闭’异常。
    出现这个异常有哪几种情况??
    服务器是正常的