我真的需要一些帮助才能从 FTP 传输更改为 SFTP。我在评论中添加了一些行,但我是编程新手,我不确定我还需要添加什么来解决这个问题。该程序工作正常,但目前您通过 FTP 传输数据。我在这里阅读了一些问题,但目前我找不到将程序重写为 SFTP 的正确提示。
public class FTP
{
private System.ComponentModel.BackgroundWorker bw = null;
private long filesSize = 0;
private long uploadSize = 0;
private const int bufferLength = 2048;
public FTP()
{
}
public FTP(System.ComponentModel.BackgroundWorker thread, long allFilesSize)
{
bw = thread;
filesSize = allFilesSize;
}
public string ReadUserFile()
{
WebClient req = new WebClient();
req.Credentials = new NetworkCredential("", "");
try
{
byte[] newFileData = req.DownloadData();
return System.Text.Encoding.Default.GetString(newFileData);
}
catch
{
return null;
}
}
public void UploadFile(
string ftpServer, string filePath, string username, string password)
{
//Create SFTP request
//Sftp client = new Sftp();
//client.Connect(hostname);
//client.Login(username, password);
//Create FTP request
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create();
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
//Load the file
//
//Load the file
FileStream stream = File.OpenRead(filePath);
//Upload to Sftp Server
//client.PutFile();
//Upload file
Stream reqStream = request.GetRequestStream();
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
reqStream.Write(buffer, 0, readBytes);
count += readBytes;
if (bw != null)
bw.ReportProgress(CalculateProgress());
}
while (readBytes != 0);
//Disconnect
//client.Disconnect();
stream.Close();
reqStream.Close();
}
private int CalculateProgress()
{
uploadSize += bufferLength;
return (Int32)(uploadSize/(filesSize / 100));
}
public void DeleteFile(
string ftpServer, string filePath, string username, string password)
{
//Create SFTP request
//Sftp client = new Sftp();
//client.Connect(hostname);
//client.Login(username, password);
//Create FTP request
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create();
request.Method = WebRequestMethods.Ftp.DeleteFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
public string[] GetFileList(
string ftpServer, string username, string password)
{
string[] fileList;
StringBuilder result = new StringBuilder();
FtpWebRequest request;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri());
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
if (line.StartsWith("./"))
line = line.Substring(2, line.Length - 2);
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch
{
//Error
fileList = null;
return fileList;
}
}
最佳答案
如果您当前正在使用 .NET FtpWebRequest
API,则没有简单的方法可以在 C#/.NET 中从 FTP 切换到 SFTP。
.NET 框架不支持 SFTP。 您需要第3方库:SFTP Libraries for .NET .
这也意味着您基本上需要从头开始修改当前的代码。当前代码中的任何一行对于使用任何第 3 方库的 SFTP 都是有用的,因为它们对于异常的 FtpWebRequest
具有非常不同的 API。
有些库为 FTP 和 SFTP 协议(protocol)提供统一的接口(interface)。
例如WinSCP .NET assembly支持 FTP、FTPS、FTPES、SFTP 等(SCP、S3、 WebDAV 和 WebDAVS)通过同一接口(interface)。
尽管它不是 native .NET 程序集。这是一个相当薄的 wrapper 控制台应用程序。
(我是 WinSCP 的作者)。
关于c# - 从 C# FtpWebRequest FTP 代码更改为 SFTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68754207/