深圳全飞鸿
标题:
C#操作sftp , 通过winSCP
[打印本页]
作者:
zhgc
时间:
2019-6-27 16:56
标题:
C#操作sftp , 通过winSCP
本帖最后由 zhgc 于 2019-6-27 16:59 编辑
定义部分:
const string FINGER_PRINT = "ssh-rsa 2048 cd:79:eb:e5:e9:2e:d6:a2:9c:79:65:2a:27:c5:1b:ba";
const string SERVER_IP = "sftp.nagomes.com";
const string SERVER_USER = "2L4I9I04F";
const string SERVER_PASSWORD = "";
const string SERVER_HOME = @"/incomingkeys/";
Session WisSession = new Session();
SessionOptions Lab126SessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = SERVER_IP,
UserName = SERVER_USER,
SshPrivateKeyPath = @"D:\sftp\private.ppk",
GiveUpSecurityAndAcceptAnySshHostKey = true,
SshHostKeyFingerprint = FINGER_PRINT,
};
复制代码
连接部分:
private void btnReconnect_Click(object sender, EventArgs e)
{
WisSession.Abort();
WisSession.Dispose();
WisSession = new Session();
WisSession.Open(Lab126SessionOptions);
}
复制代码
文件上传:
public void PutFile(string sourcePath, string targetPath)
{
{
string[] files = Directory.GetFiles(sourcePath);
if (files.Count() == 0)
{
return;
}
TransferOptions options = new TransferOptions();
TransferOperationResult tfr;
options.TransferMode = TransferMode.Binary;
tfr = WisSession.PutFiles(sourcePath + "*", targetPath + @"*", true, options);
tfr.Check();
}
}
复制代码
目录遍历:
public List<string> ListFiles(string dir)
{
List<string> fileList = new List<string>();
RemoteDirectoryInfo rd;
rd = WisSession.ListDirectory(dir);
foreach (RemoteFileInfo rf in rd.Files)
{
if (rf.Name.Trim() == "." || rf.Name.Trim() == "..")
{
continue;
}
fileList.Add(rf.Name);
}
fileList.Sort();
return fileList;
}
复制代码
文件下载:
public void Getfile(string sourcePath, string targetPath)
{
TransferOperationResult tfr;
tfr = WisSession.GetFiles(sourcePath + "*", targetPath + "*", true);
tfr.Check();
}
复制代码
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2