Search code examples
c#fluentftp

Fluent ftp get latest files and download


I need to bring back the files of today which are csv files to a local directory I was using this example from fluent ftp web site but its not working its finding the /in/ directory ok and there is a test file in there but its not downloading the file.

public  void GetListing()
{
        using (FtpClient conn = new FtpClient())
        {
            conn.Host = ftpIpAddress;
            conn.Credentials = new NetworkCredential(ftpUserName,ftpPassword);

            foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
                FtpListOption.Modify | FtpListOption.Size))
            {

                switch (item.Type)
                {
                    case FtpFileSystemObjectType.Directory:
                        break;
                    case FtpFileSystemObjectType.File:
                        Console.Write("Filename " + item.FullName);
                        conn.DownloadFile(item.FullName,"/in/");
                        break;
                    case FtpFileSystemObjectType.Link:
                        // derefernece symbolic links
                        if (item.LinkTarget != null)
                        {
                            // see the DereferenceLink() example
                            // for more details about resolving links.
                            item.LinkObject = conn.DereferenceLink(item);

                            if (item.LinkObject != null)
                            {
                                // switch (item.LinkObject.Type)...
                            }
                        }
                        break;
                }
            }

            // same example except automatically dereference symbolic links.
            // see the DereferenceLink() example for more details about resolving links.
            foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
                FtpListOption.Modify | FtpListOption.Size | FtpListOption.DerefLinks))
            {

                switch (item.Type)
                {
                    case FtpFileSystemObjectType.Directory:
                        break;
                    case FtpFileSystemObjectType.File:
                        Console.Write("File " + item.FullName);
                        break;
                    case FtpFileSystemObjectType.Link:
                        if (item.LinkObject != null)
                        {
                            // switch (item.LinkObject.Type)...
                        }
                        break;
                }
            }
        }
  }

I have setup a test envoriment with filezilla server on 127.0.1 I am passing my details to my class below as such

ServerConnection connection = new ServerConnection();
connection.ftpIpAddress = "127.0.0.1";
connection.ftpUserName = "ftpuser";
connection.ftpPassword = "ftppassword";
connection.LocalDestDirectory = @"C:\ImportedFromPump\in";
connection.remoteDirectory = @"\in\";

I want to stored the files in the local directory value and also when they have been downloaded I wish to delete them from the ftp but I am not to sure how to go about that.

I have been following this tutorial here.

https://github.com/robinrodricks/FluentFTP/blob/master/FluentFTP.Examples/GetListing.cs

Results from Debug Info:

# OpenPassiveDataStream(AutoPassive, "MLSD /", 0)
Command:  EPSV
Response: 229 Entering Extended Passive Mode (|||63478|)
Status:   Connecting to 127.0.0.1:63478
Command:  MLSD /
Response: 150 Opening data channel for directory listing of "/"
+---------------------------------------+
Listing:  type=dir;modify=20181018113309; Archived
Listing:  type=dir;modify=20181018115328; in
-----------------------------------------
Status:   Disposing FtpSocketStream...

# CloseDataStream()
Response: 226 Successfully transferred "/"
Status:   Disposing FtpSocketStream...

# Dispose()
Status:   Disposing FtpClient object...
Command:  QUIT
Response: 221 Goodbye
Status:   Disposing FtpSocketStream...
Status:   Disposing FtpSocketStream...

Edit 2

Ok so I am getting a bit futher and changed my code to use download file but now I am getting an accessed denied message.

public void GetListing()
{
        try
        {
            using (FtpClient conn = new FtpClient())
            {
                IEnumerable<string> directorys = new[] { "/in" };

                conn.Host = ftpIpAddress;
                conn.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

                FtpListItem[] files = conn.GetListing("/in/", FtpListOption.AllFiles)
            .Where(x => x.Type == FtpFileSystemObjectType.File)
            .OrderBy(x => x.Modified)
            .ToArray();

                foreach(FtpListItem file in files)
                {

                    conn.DownloadFile(Environment.GetEnvironmentVariable("LocalAppData") + @"\Fuel\",file.FullName, true);

                }


            }
        }
        catch
        (Exception ex)
        {


        }
}

The errror is here

DownloadFile("C:\Users\user\AppData\Local\Fuel\", "/in/FuelPumpData.csv", True, None) Exception thrown:

'System.IO.DirectoryNotFoundException' in mscorlib.dll

Even thought it a user folder and it exist what the heck is going on.

Edit 2 To prove that the directory exists. enter image description here

Edit 3 To Show that the ftp directory exists

enter image description here

Edit 4

To Prove that the .net object is finding the file in the code.

enter image description here

Edit 5 To Show the directory permissions. enter image description here

Edit 6

enter image description here


Solution

  • The first argument to FtpClient.DownloadFile() is a filename, not a directory name. If you provide a directory name, the operation fails with that exception, as you can't open a FileStream on a directory (which is what the FluentFTP library is doing internally).

    You could for example build the target filename like this:

    var localFile = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "Fuel", file.Name);