【问题标题】:ssh.net check file extensionssh.net 检查文件扩展名
【发布时间】:2015-02-11 05:32:56
【问题描述】:
我使用 SSH.NET 库连接到 sftp 服务器并设法从服务器下载文件。在服务器中有很多文件类型,我只需要 CSV 文件即可下载。如何检查文件扩展名 GetExtension 在 SSH.NET 中不起作用。非常感谢帮助。
foreach (var file in files)
{
if (!file.Name.StartsWith("."))
{
string remoteFileName = file.Name;
using (Stream fileStream = System.IO.File.OpenWrite(Path.Combine(sFilePath, file.Name)))
{
sftp.DownloadFile(file.FullName, fileStream);
}
}
}
【问题讨论】:
标签:
asp.net-mvc
visual-studio
ssh.net
【解决方案1】:
我有一个下载大量以“.rpt”结尾的文件的进程。就我而言,我关心最新的文件,但无论哪种方式,这段代码都能正常工作。
ConnectionInfo c = new PasswordConnectionInfo(host, port, userName, password);
var sftp = new SftpClient(c);
List<SftpFile> allFiles = sftp.ListDirectory(directory).ToList().OrderByDescending(f => f.LastWriteTime).ToList();
var fileNames = allFiles.Where(f => f.Name.EndsWith("csv")).Select(f => f.Name).ToList();
【解决方案2】:
我找到了一个解决方案,我将其发布在这里,以帮助像我一样遇到同样问题的任何人。
string fname = file.Name;
string ext = fname.Substring(fname.Length - 4, 4);
if (ext == ".csv")
{
//code goes here
}
【解决方案3】:
像...这样的东西怎么样
ConnectionInfo c = new PasswordConnectionInfo(host,port,username,password);
var sftp = new SftpClient(c);
string remote= "/FromServer/"
sftp.Connection();
Console.WriteLine("Connected to {0}", host); // Just to help keep track of things
sftp.ChangeDirectory(remote);
Console.WriteLine("Changed directory to {0}", remote);// You should see your repository with this
var allFilenames = Directory.EnumerateFiles(remote).Select(p => Path.GetFileName(p));
var withext = allFilenames
.Where(fn => Path
.GetExtension(fn) == ".csv")
.Select(fn => Path.GetFileName(fn));
记得也添加这些人...
using Renci.SshNet;
using Renci.SshNet.Sftp;
希望这会有所帮助!! :)