winSCP 本地项目访问外系统文件夹下面的文件

时间:2022-05-27 08:31:19

string hostName = ConfigurationManager.AppSettings["HostName"];
string userName = ConfigurationManager.AppSettings["UserName"];
string password = ConfigurationManager.AppSettings["Password"];
string SKey = ConfigurationManager.AppSettings["SshHostKeyFingerprint"];
string kex = ConfigurationManager.AppSettings["KEX"];
string sourcePath = ConfigurationManager.AppSettings["FromPath"];
string extenName = ConfigurationManager.AppSettings["FileName"];
string hostPath = ConfigurationManager.AppSettings["ToPath"];

try
{
// 配置会话选项
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Scp,
HostName = hostName,
UserName = userName,
Password = password,
SshHostKeyFingerprint = SKey,
};
sessionOptions.AddRawSettings("KEX", kex);
using (Session session = new Session())
{
session.Open(sessionOptions);
var commandResult = session.ExecuteCommand("cd " + sourcePath);
commandResult = session.ExecuteCommand("ls");
if (commandResult.IsSuccess)
{
var fileNames = commandResult.Output.Split(‘\n‘).Where(fileName => fileName.EndsWith(extenName)).ToList();
if (!Directory.Exists(hostPath))
{
Directory.CreateDirectory(hostPath);
}
foreach (var fileName in fileNames)
{
var result = session.GetFiles("http://www.mamicode.com/" + sourcePath + "http://www.mamicode.com/" + fileName, hostPath);
//Console.WriteLine(string.Format("FileName:{0},文件传输{1}!", fileName, result.IsSuccess ? "成功" : "失败"));
}
}
}
// Console.WriteLine("完成");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

winSCP 本地项目访问外系统文件夹下面的文件