Achieving FTP/SFTP without 3rd party dll with FtpWebRequest if possible in C#

前端 未结 3 2045
梦如初夏
梦如初夏 2021-02-04 06:56

I am trying to achieve the ftp/sftp through FtpWebRequest class in C# but not succeeded till now.

I dont want to use any 3rd party free or paid dll.

credentials

相关标签:
3条回答
  • 2021-02-04 07:27

    Agreed with Tejs. Just to clarify:

    FtpWebRequest with EnableSsl = true means it's ftps, Explicit mode, or in Filezilla wording: "FTPES - FTP over Explicit TLS/SSL, default port 21". You can do this with the built-in .net stuff.

    For Implicit ftps (in Filezilla wording "FTPS - FTP over Implicit TLS/SSL, default port 990") you have to use a 3rd party (example ftps.codeplex.com).

    For sftp (in Filezilla wording "SSH File Transfer Protocol, default port 22") you also have to use 3rd party (example sshnet.codeplex.com).

    As Joachim Isaksson says, if you can't use 3rd parties, you have to implement it yourself.

    0 讨论(0)
  • 2021-02-04 07:33

    UPDATE:

    If using BizTalk, you can work with the SFTP Adapter, using the ESB Toolkit. It has been supported since 2010. One wonders why it didn't make it to the .Net Framework

    1. BizTalk Server 2013: Creating Custom Adapter Provider in ESB Toolkit SFTP As an Example
    2. BizTalk Server 2013: How to use SFTP Adapter
    3. MSDN Docs

    --

    Unfortunately, it still requires alot of work to do with just the Framework currently. Placing the sftp protocol prefix isnt enough to make-it-work Still no built-in, .Net Framework support today, maybe in the future.

    ---------------------------------------------------------

    1) A good library to try out is SSHNet.

    ---------------------------------------------------------

    It has:

    1. a lot more features, including built-in streaming support.
    2. an API document
    3. a simpler API to code against

    Example code from documentation:

    List directory

    /// <summary>
    /// This sample will list the contents of the current directory.
    /// </summary>
    public void ListDirectory()
    {
        string host            = "";
        string username        = "";
        string password        = "";
        string remoteDirectory = "."; // . always refers to the current directory.
    
        using (var sftp = new SftpClient(host, username, password))
        {
            sftp.Connect();
    
            var files = sftp.ListDirectory(remoteDirectory);
            foreach (var file in files)
            {
                Console.WriteLine(file.FullName);
            }
        }
    }
    

    Upload File

    /// <summary>
    /// This sample will upload a file on your local machine to the remote system.
    /// </summary>
    public void UploadFile()
    {
        string host           = "";
        string username       = "";
        string password       = "";
        string localFileName  = "";
        string remoteFileName = System.IO.Path.GetFileName(localFile);
    
        using (var sftp = new SftpClient(host, username, password))
        {
            sftp.Connect();
    
            using (var file = File.OpenRead(localFileName))
            {
                sftp.UploadFile(remoteFileName, file);
            }
    
            sftp.Disconnect();
        }
    }
    

    Download File

    /// <summary>
    /// This sample will download a file on the remote system to your local machine.
    /// </summary>
    public void DownloadFile()
    {
        string host           = "";
        string username       = "";
        string password       = "";
        string localFileName  = System.IO.Path.GetFileName(localFile);
        string remoteFileName = "";
    
        using (var sftp = new SftpClient(host, username, password))
        {
            sftp.Connect();
    
            using (var file = File.OpenWrite(localFileName))
            {
                sftp.DownloadFile(remoteFileName, file);
            }
    
            sftp.Disconnect();
        }
    }
    

    ---------------------------------------------------------

    2) Another alternative library is WinSCP

    ---------------------------------------------------------

    With the follwing example:

    using System;
    using WinSCP;
    
    class Example
    {
        public static int Main()
        {
            try
            {
                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Sftp,
                    HostName = "example.com",
                    UserName = "user",
                    Password = "mypassword",
                    SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
                };
    
                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);
    
                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
    
                    TransferOperationResult transferResult;
                    transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, 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;
            }
        }
    }
    

    Found here and with more here.

    0 讨论(0)
  • 2021-02-04 07:33

    FTP can be accomplished with .NET alone. There is no built in class for SFTP though. I would recommend taking a look at WinSCP.

    0 讨论(0)
提交回复
热议问题