从 C# FtpWebRequest FTP 代码更改为 SFTP

Change from C# FtpWebRequest FTP code to SFTP

我真的需要一些帮助才能从 FTP 转移到 SFTP。我在评论中添加了一些行,但我是编程新手,我不确定我还需要添加什么来解决这个问题。该程序运行良好,但此时您通过 FTP 传输数据。我在这里读了一些问题,但现在,我找不到合适的 tipp 将程序重写为 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;
        }
    }

在 C#/.NET 中,没有从 FTP 切换到 SFTP 的简单方法,如果您当前使用的是 .NET FtpWebRequest API。

.NET 框架不支持 SFTP。 您需要第 3 方库:SFTP Libraries for .NET.

这也意味着您基本上需要从头开始编写当前代码。当前代码中没有一行对任何第 3 方库的 SFTP 有用,因为它们 API 与不寻常的 FtpWebRequest.

有很大不同

有些库为 FTP 和 SFTP 协议提供统一的接口。

例如WinSCP .NET assembly支持FTP、FTPS、FTPES、SFTP等(SCP、S3、 WebDAV 和 WebDAVS)在同一接口上。

尽管它不是本机 .NET 程序集。这是一个薄包装 控制台应用程序。

(我是WinSCP的作者)