4 Replies - 435 Views - Last Post: 14 June 2013 - 11:53 AM Rate Topic: -----

#1 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 446
  • Joined: 10-October 09

Changing directories in FTP Program

Posted 13 June 2013 - 01:35 PM

I have been stumped on this issue for quite some time now.. What I have is a list view filled with directories in the root folder of an FTP server I have made. The program connects to my FTP server just fine, and lists the root directories. I then added a method that would switch to the selected directory whenever it is double clicked. The click event works fine, but whenever it tries to connect to the new directory, I get an error saying "No such host exists". This is really annoying, and I don't understand how to work around it. All I want is for my listview to be cleared, and repopulated with the contents of the subdirectory... How can one achieve this?

Here is my code:
public void ChangeDirectory(string dirName)
        {
            lstFiles.Items.Clear();
            using (FtpClient conn = new FtpClient())
            {
                conn.Host = Project_Management.Properties.Settings.Default["FTPServer"].ToString() + "/$2f" + dirName.ToString() + "/";
                conn.Credentials = new NetworkCredential(Project_Management.Properties.Settings.Default["FTPUser"].ToString(), Project_Management.Properties.Settings.Default["FTPPass"].ToString());
                try
                {
                    foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
        FtpListOption.Modify | FtpListOption.Size))
                    {

                        switch (item.Type)
                        {
                            case FtpFileSystemObjectType.Directory:
                                lstFiles.Items.Add(item.Name);
                                break;
                            case FtpFileSystemObjectType.File:
                                lstFiles.Items.Add(item.Name);
                                break;
                        }
                    }
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
        }



I am using the System.Net.FtpClient library that I downloaded awhile back, but it still pretty much works almsost exactly like FtpWebRequest.

Edit: I am willing to switch back to FtpWebRequest if this is what you understand.. As long as I can figure out how this is done.

This post has been edited by optix212: 13 June 2013 - 01:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Changing directories in FTP Program

#2 cokacola  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 62
  • Joined: 23-July 10

Re: Changing directories in FTP Program

Posted 13 June 2013 - 06:16 PM

Instead of appending the new directory onto the end of the Host, inside the try/catch, before your foreach loop, try using conn.SetWorkingDirectory("/path/goes/here");
conn.SetWorkingDirectory("/$2f" + dirName.ToString() + "/");

Was This Post Helpful? 0
  • +
  • -

#3 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 446
  • Joined: 10-October 09

Re: Changing directories in FTP Program

Posted 13 June 2013 - 06:48 PM

I went ahead and downloaded another api that can be found here .

Just letting everyone know for future reference, that the default FtpWebRequest is not enough to run basic FTP commands. You will need to download an outside library to run FTP commands.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,531
  • Joined: 05-May 12

Re: Changing directories in FTP Program

Posted 14 June 2013 - 07:40 AM

Also, dirName is already string. There is no need to call ToString() on it.
Was This Post Helpful? 0
  • +
  • -

#5 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 446
  • Joined: 10-October 09

Re: Changing directories in FTP Program

Posted 14 June 2013 - 11:53 AM

I always be sure to call .ToString() even if it is a string, just to be safe.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1