1

I would use this instruction:

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("ftp://192.168.47.1/DocXML");

But I can´t.

How can I use ("ftp://192.168.47.1/DocXML"); with new System.IO.DirectoryInfo("");?

This is the code

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"\\192.168.47.1\DocXML");`

IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
garci
  • 87
  • 3
  • 8

3 Answers3

1

I'm afraid you can't.

Try this instead:

FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://192.168.47.1/DocXML");
req.Credentials = new NetworkCredential("foo", "foo@foo.com");
req.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
using (StreamReader streamReader = new StreamReader(res.GetResponseStream()))
{
...
}
Leonardo
  • 186
  • 8
  • and how I can download the file with the oldest creation date from the FTP server? – garci May 16 '15 at 15:45
  • Parse the stream, it should contain the whole files listing. Then, using FtpWebRequest/FtpWebResponse, download the oldest one. Refer to this: [link](https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx) – Leonardo May 16 '15 at 15:57
  • but I want to know the oldest file from a FTP server carpet and FtpWebRequest/FtpWebResponse does not help me. – garci May 16 '15 at 16:17
  • Sorry, use `req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;` and parse the response to select the oldest one, then download it. – Leonardo May 16 '15 at 16:28
1

If you a need structured information about files in an FTP directory, you have to use a 3rd party library. The .NET framework does not offer such functionality.

Particularly because it does not support an MLSD FTP command, what is the only reliable way to retrieve a machine-readable listing of remote files with their attributes.


There are many 3rd party libraries that allow this.

For example with WinSCP .NET assembly:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "username",
    Password = "password",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Get list of files in the directory
    string remotePath = "/remote/path/";
    RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);

    foreach (RemoteFileInfo fileInfo in directoryInfo.Files)
    {
        Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
            fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
            fileInfo.LastWriteTime);
    }
}

References:
https://winscp.net/eng/docs/library_session_listdirectory
https://winscp.net/eng/docs/library_remotefileinfo

From your comment and your other question, you seem to actually need to retrieve the oldest file in FTP directory. For that see:

Both are for the newest, not oldest, file. Just replace the .OrderByDescending with the .Order in the C# code to get the oldest file.

(I'm the author of WinSCP)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

Not working in this way. I recommend using SFTP instead of FTP. For this I'm using the 3rd party library "SharpSSH". The following example seems to work:

using System.IO;
using Tamir.SharpSsh;
using Tamir.SharpSsh.jsch;

string ip = "DestinationIp";
string user = "JohnDoe";
string password = "YourPassword";
Sftp sftp = new Tamir.SharpSsh.Sftp(ip, user, password);
sftp.Connect();

FileInfo yourFileInfo = new FileInfo("path");

There's also the possibility to add a primary key with sftp.AddIdentityFile();