Users Pricing

forum

Home Forums Why this code is repeating files in listbox? – MindStick

Why this code is repeating files in listbox?

Anonymous User 1998 18 Aug 2014

I want to list all files from my ftp folder, I am using this code. But it gives me twice the name of files. What is wrong with it?

private void ListFilesOnServer()
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ConfigurationSettings.AppSettings.Get("IncomingFtpPath"));
                request.Credentials = new NetworkCredential("user", "password");
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (System.IO.Path.GetExtension(line) == ".xml")
                    {
                        WaitingListBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(line));
                    }
                }
                reader.Close();
                response.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }


1 Answers

Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md