ssh.net 检查文件扩展名
ssh.net check file extension
我使用 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);
}
}
}
我找到了一个解决方案,我将其发布在这里,以帮助任何和我有同样问题的人。
string fname = file.Name;
string ext = fname.Substring(fname.Length - 4, 4);
if (ext == ".csv")
{
//code goes here
}
我有一个进程会下载很多以“.rpt”结尾的文件。就我而言,我关心的是最新的文件,但无论哪种方式,这段代码都能很好地工作。
ConnectionInfo c = new PasswordConnectionInfo(主机、端口、用户名、密码);
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();
怎么样...
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;
希望对您有所帮助!! :)
我使用 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);
}
}
}
我找到了一个解决方案,我将其发布在这里,以帮助任何和我有同样问题的人。
string fname = file.Name;
string ext = fname.Substring(fname.Length - 4, 4);
if (ext == ".csv")
{
//code goes here
}
我有一个进程会下载很多以“.rpt”结尾的文件。就我而言,我关心的是最新的文件,但无论哪种方式,这段代码都能很好地工作。 ConnectionInfo c = new PasswordConnectionInfo(主机、端口、用户名、密码);
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();
怎么样...
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;
希望对您有所帮助!! :)