Kate
Kate

Reputation: 372

List of files from ftp

i have problem with this code..the code give me back this names of files:

"."
"orders00001.xml"
".."
"orders00010.xml" 

But in the folder are only order00001 and order 00010.xml. Have you any idea where is the problem please?

private void getFileList()
            {
                List<string> files = new List<string>();
                try
                {
                    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(****);
                    request.Method = WebRequestMethods.Ftp.ListDirectory;
                    request.Credentials = new NetworkCredential(**, **);
                    request.UsePassive = true;
                    request.UseBinary = true;
                    request.KeepAlive = false;
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream);
                    while (!reader.EndOfStream)
                    {
                        Application.DoEvents();
                        files.Add(reader.ReadLine());
                    }
                    reader.Close();
                    responseStream.Close(); //redundant
                    response.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("error connecting");
                }
                if (files.Count != 0)
                {
                    foreach (string file in files)
                    {
                     //My code on work with xml
                    }
                else
                {
                    getFileList();
                }
            }

Upvotes: 0

Views: 822

Answers (1)

Samuel
Samuel

Reputation: 6500

Directory management lists . and .. as virtual directories. The directory . points to itself allowing to refresh the directory. The directory .. directs you one directory up. Filter these two directory entries when parsing your orders.

You may have seen directory paths like c:\windows..\Users which actually points to c:\Users as .. goes one directory hierarchy up.

Having a path pointing to .\Users means the directory Users in the current(.) directory.

You should always filter them out because if you are writing a recursive algorithm reading the directory "." will result in an infinite loop.

Upvotes: 1

Related Questions