1. 程式人生 > 實用技巧 >基於WinSCP實現SFTP/FTP檔案的上傳下載

基於WinSCP實現SFTP/FTP檔案的上傳下載

介紹

WinSCP .NET 程式集是Nuget上的安裝包,它允許程式碼連線到遠端計算機,並在 SFTP、FTP、WebDAV、S3 和 SCP 會話上操作遠端檔案

官網資訊

地址

https://winscp.net/eng/index.php

.Net示例程式碼

https://winscp.net/eng/docs/library_examples

SFTP操作檔案

上傳 PutFiles
public static int WinScpUpload()
{
    try
    {
        // Setup session options
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
            HostName = "192.168.xx.xxx",
            PortNumber = 22,// SFTP預設埠22
            UserName = "使用者名稱",
            Password = "密碼",
            SshHostKeyFingerprint = "ssh-ed25519 255 ***"// 不知道時可以在下面根據使用者名稱和密碼生成,是必須的
        };

        using (Session session = new Session())
        {
            // 可以使用該語句生成SshHostKeyFingerprint
            //var fingerprint = session.ScanFingerprint(sessionOptions, "SHA-256");

            // Connect
            session.Open(sessionOptions);

            // Upload files
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;

            // PutFiles引數解釋:localPath,remotePath,remove,TransferOptions
            // remove:bool,成功後是否刪除本地檔案,預設false
            var transferResult = session.PutFiles(@"d:\test\*", "/home/test/", true, transferOptions);

            // Throw on any error
            transferResult.Check();

            // Print results
            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
            }
        }

        return 0;
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: {0}", e);
        return 1;
    }
}
下載 GetFiles

下載檔案與上傳檔案程式碼基本一致,對應的PutFiles改為GetFiles即可

FTP操作檔案

上傳 PutFiles
public static int WinScpUpload()
{
    try
    {
        // Setup session options
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Ftp,
            HostName = "192.168.xx.xxx",
            PortNumber = 21,// FTP預設埠21
            UserName = "使用者名稱",
            Password = "密碼"
        };

        using (Session session = new Session())
        {
            // Connect
            session.Open(sessionOptions);

            // Upload files
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;
            
            // PutFiles引數解釋:localPath,remotePath,remove,TransferOptions
            // remove:bool,成功後是否刪除本地檔案,預設false
            var transferResult = session.PutFiles(@"d:\test\*", "/home/test/", true, transferOptions);

            // Throw on any error
            transferResult.Check();

            // Print results
            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
            }
        }

        return 0;
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: {0}", e);
        return 1;
    }
}
下載 GetFiles

下載檔案與上傳檔案程式碼基本一致,對應的PutFiles改為GetFiles即可

// GetFiles引數解釋:remotePath,localPath,remove,TransferOptions
// remove:bool,成功後是否刪除遠端檔案,預設false
var transferResult = session.GetFiles("/home/test.yao/test.txt", @"d:\download\", true, transferOptions);

總結

  • SFTP:預設埠22,協議為SFTP,需要有SshHostKeyFingerprint
  • FTP
    :預設埠21,協議為FTP,不需要SshHostKeyFingerprint