---
title: "Why this code is repeating files in listbox?"  
description: "Why this code is repeating files in listbox?"  
author: "Anonymous User"  
published: 2014-08-18  
updated: 2014-08-18  
canonical: https://www.mindstick.com/forum/2049/why-this-code-is-repeating-files-in-listbox  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Why this code is repeating files in listbox?

I want to list all [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) from my [ftp](https://www.mindstick.com/articles/33718/ftp-vs-sftp-understanding-these-two-meanings) folder, I am using this code. But it gives me twice the [name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide) of files. What is [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when) 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);            }        }
```

## Replies

### Reply by Pravesh Singh

Hi Tanuj, \

```
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();           
using(StreamReader reader = new StreamReader(response.GetResponseStream())            {               string line = null;              
while((line = reader.ReadLine()) != null)               {                if (System.IO.Path.GetExtension(line) == ".xml")                {                 
WaitingListBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(line));                }               }            }        }        catch (Exception e)        {           
MessageBox.Show(e.Message);            // throw e        }
```


---

Original Source: https://www.mindstick.com/forum/2049/why-this-code-is-repeating-files-in-listbox

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
