从FTP服务器下载最新文件
我必须从FTP服务器下载最新的文件。 我知道如何从我的电脑下载最新文件,但我不知道如何从FTP服务器下载。
如何从FTP服务器下载最新文件?
这是我从我的电脑下载最新文件的程序
public Form1() { InitializeComponent(); string startFolder = @"C:Usersuser3DesktopDocumentos XML"; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder); IEnumerable fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories); IEnumerable fileQuerry = from file in fileList where file.Extension == ".txt" orderby file.CreationTimeUtc select file; foreach (System.IO.FileInfo fi in fileQuerry) { var newestFile = (from file in fileQuerry orderby file.CreationTimeUtc select new { file.FullName, file.Name }) .First(); textBox2.Text = newestFile.FullName; } }
好的,用这段代码我知道最后一个文件的日期,但我怎么知道这个文件的名字大全????????
您必须检索远程文件的时间戳以选择最新的文件。
遗憾的是,使用.NET框架提供的function,没有真正可靠有效的方法来检索目录中所有文件的修改时间戳,因为它不支持FTP MLSD
命令。 MLSD
命令以标准化的机器可读格式提供远程目录的列表。 RFC 3659标准化了命令和格式。
您可以使用的替代方案,.NET框架支持:
或者,您可以使用支持现代MLSD
命令的第三方FTP客户端实现。
例如, WinSCP .NET程序集支持它。
甚至还有一个特定任务的示例: 下载最新文件 。
该示例适用于PowerShell和SFTP,但很容易转换为C#和FTP:
// 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); // Select the most recent file RemoteFileInfo latest = directoryInfo.Files .OrderByDescending(file => file.LastWriteTime) .First(); // Download the selected file string localPath = @"C:localpath"; string sourcePath = RemotePath.EscapeFileMask(remotePath + latest.Name); session.GetFiles(sourcePath, localPath).Check(); }
(我是WinSCP的作者)
上述就是C#学习教程:从FTP服务器下载最新文件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1025196.html