使用WinSCP .NET程序集检查任何.xls
文件是否在远程SFTP文件夹中的C#代码是什么? xls
文件具有不同的名称,我只知道我想要的文件将是xls
文件扩展名。
string strFileRemote = @"/folder/*.xls";
// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = sftpHost,
UserName = sftpUsername,
SshHostKeyFingerprint = "value for finger print",
SshPrivateKeyPath = @"\\file server\.ppk",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
// Download
transferResult =
session.GetFiles(strFileRemote, strFileLocal, false, transferOptions);
// Throw on any error
transferResult.Check();
if (transferResult.IsSuccess == false)
{
strStatus = "Error: " + transferResult.ToString();
}
}
答案 0 :(得分:1)
根据WinSCP article on Checking file existence的建议,如果在SFTP服务器上存在与掩码匹配的文件,请使用Session.EnumerateRemoteFiles
进行检查:
bool anyXmlExists =
session.EnumerateRemoteFiles("/folder", "*.xml", EnumerationOptions.None).Any();