class FtpUpDown
        {
            string ftpServerIP;
            string ftpUserID;
            string ftpPassword;
            FtpWebRequest reqFTP;
            private void Connect(String path)//连接ftp
            {
                // 根据uri创建FtpWebRequest对象
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
                // 指定数据传输类型
                reqFTP.UseBinary = true;
                // ftp用户名和密码
                reqFTP.Credentials = new NetworkCredential("spftp", "SPFTPspftp123");
            }
            public FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)
            {
                this.ftpServerIP = ftpServerIP;
                this.ftpUserID = ftpUserID;
                this.ftpPassword = ftpPassword;            }
            //都调用这个
            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.Default);//中文文件名
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n ");
                        line = reader.ReadLine();
                    }
                    // to remove the trailing '' ''
                    //result.Remove(result.ToString().LastIndexOf(""), 1);
                    reader.Close();
                    response.Close();
                    return result.ToString().Split('\n');
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                    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);            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
           try
           {
                FtpUpDown ftpUpDown = new FtpUpDown("123.139.155.169", "spftp ", "SPFTPspftp123");
                ftpUpDown.GetFileList("/VAC/SubscribeInfo/81559/add/request");
                string[] str = ftpUpDown.GetFileList("/VAC/SubscribeInfo/81559/add/request");
                listBox1.Items.Clear();
                listBox1.Items.AddRange(str);
           }           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, " Error");
           }        }

解决方案 »

  1.   

    总是运行三四次就报远程服务器返回错误;(500)语法错误,我调试了一下,在
    private void button1_Click(object sender, EventArgs e)
       {
       try
       {
       FtpUpDown ftpUpDown = new FtpUpDown("123.139.155.169", "spftp ", "SPFTPspftp123");
       ftpUpDown.GetFileList("/VAC/SubscribeInfo/81559/add/request");《《《《《《《运行三四次后这个地方取值为null了!
     
      

  2.   

    你跑到GetFileList函数里去看看哪里出错
      

  3.   

    有一个老办法你可以尝试
    就是将ftp操作写到bat文件中
    在程序中调用bat文件
      

  4.   

    你进到GetFileList函数没有?单步调试到 ftpUpDown.GetFileList("/VAC/SubscribeInfo/81559/add/request");这句话时,按F11跳进去,再单步上面那个语句出错,也有个具体位置把
      

  5.   

    很清楚了
    单步调试到 ftpUpDown.GetFileList("/VAC/SubscribeInfo/81559/add/request");这句话时,按F11跳进去,再单步调试(逐语句调试)GetFileList函数啊,看那个语句出问题了。很清楚饿了
      

  6.   

    高手,你说的GetFileList函数是第一个还是第二个?
      

  7.   

    晕。你调用哪个就是那个, //都调用这个
      private string[] GetFileList(string path, string WRMethods)//上面的代码示例了如何
    你按F11它会自动跳到你调用的那个函数
      

  8.   

    你把你的  ftp:ip 用户名 密码 全暴露了,  不怕服务器被黑啊