【发布时间】:2019-10-23 13:51:33
【问题描述】:
我必须更改旧的 C# 脚本(早在 2010 年在 C# Express 中创建)以使用 SFTP 而不是 FTP。从旧存储库中获取此脚本需要一些工作 - 并下载 C# 2010 Express 版本以打开脚本!但我终于做到了。我从来没有用 C# 编写过代码,所以请善待。我们有几台异地 PC 需要在其网络上的服务器之间传输文件。这个脚本是一个 .dll,在这些系统上的其他几个程序中都引用了它,所以我还需要在这些脚本中更改对这个 .dll 的调用。我已经研究过在 C# 中使用 SFTP,并且从我所看到的情况来看,最好安装 Renci.SshNet,因此已将其安装为项目中的参考。我想要做的是更改下面的功能以使用 SFTP 而不是 FTP,但我不确定如何开始 -
public TimeSpan FTPTo(string strFTPServer, string strLocalFileandPath, string strFTPFileandPath, string strFTPUser, string strFTPPassword)
{
try
{
DateTime dtStart = DateTime.Now;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(strFTPServer + "/" + strFTPFileandPath);
ftp.Proxy = null;
ftp.Credentials = new NetworkCredential(strFTPUser, strFTPPassword);
//userid and password for the ftp server to given
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(strLocalFileandPath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
return DateTime.Now - dtStart;
}
catch (Exception ex1)
{
throw ex1;
}
}
任何建议/帮助将不胜感激。
【问题讨论】: