none
FTP gets file list error RRS feed

  • Question

  • private string[] GetAllList(string url)
            {
                List<string> list = new List<string>();
                FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri(url));
                req.Credentials = new NetworkCredential(ftpPassword, ftpPassword);
                req.Method = WebRequestMethods.Ftp.ListDirectory;
                req.UseBinary = true;
                req.UsePassive = false;
                try
                {
                    using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
                    {
                        using (StreamReader sr = new StreamReader(res.GetResponseStream()))
                        {
                            string s;
                            while ((s = sr.ReadLine()) != null)
                            {
                                list.Add(s);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                return list.ToArray();
            }

    Use the above method to

    s = sr.ReadLine()

    The error message prompts the host to forcibly close an existing connection, and the browser can input the path to obtain a list of files in the directory. How to solve this problem?


    please verify my account


    • Edited by lctk Thursday, July 19, 2018 3:14 AM
    Thursday, July 19, 2018 2:41 AM

All replies

  • I don't know what is problem but there is bad exception handling in your code. You use try{} catch{throw ex;} If you use throw ex exception context is removed. You can rewrite as try{} catch{throw;} but it is same as you don't use try/catch. So try/catch block in your code is unnecessary. 

    You can try to catch WebException to see error code and message.

    Thursday, July 19, 2018 4:32 AM
  • > req.Credentials = new NetworkCredential(ftpPassword, ftpPassword);

    There's a typo on that line.  Do you see it?  When I fix that, your code works for me.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Proposed as answer by Wizend Thursday, July 19, 2018 5:24 PM
    Thursday, July 19, 2018 5:15 PM
  • Nice, I didn't see it. But if there had catch WebException it would get code and unauthorize description. 
    Thursday, July 19, 2018 7:34 PM
  • No, sorry, the above code is just my reference code. I actually wrote the correct username and password. If it is a NetworkCredential error, the code will not execute to while ((s = sr.ReadLine())! = null),
    Now it is possible to connect to TCp, but when reading, the server connection is disconnected.
    I use the browser to enter the FTp address, enter the username and password, but I can see the file list.

    please verify my account

    Friday, July 20, 2018 12:16 AM
  • No, sorry, the above code is just my reference code. I actually wrote the correct username and password. If it is a NetworkCredential error, the code will not execute to while ((s = sr.ReadLine())! = null),
    Now it is possible to connect to TCp, but when reading, the server connection is disconnected.
    I use the browser to enter the FTp address, enter the username and password, but I can see the file list.

    please verify my account

    Friday, July 20, 2018 12:16 AM

  • please verify my account

    Friday, July 20, 2018 12:23 AM
  • Later, I found the reason. I can't debug in vs, but I can run the program separately, mainly because the client has opened a firewall, and the connection mode gives me a little dizzy, using UsePassive = false; Is the active mode, I still thought it was passive mode,

    please verify my account

    Friday, August 3, 2018 2:08 AM