none
How do I upload and download files from an SFTP server ? RRS feed

  • Question

  • Hello All. I did type "SFTP" in the search box and found quite a lot of stuff about SFTP. It's said that VS doesn't support SFTP naturally. But many of them were more-than-10-year threads. VS might suport SFTP now. So I want to ask the same question for the freshest answers. Does VS support SFTP now ? If not, what do you think are good third party SFTP modules and where I can down them. I use Visual Studio 2015 Community. Thank you.
    • Edited by AlanChong Tuesday, March 24, 2020 6:18 AM more info
    Tuesday, March 24, 2020 4:07 AM

Answers

  • Hi AlanChong,
    Based on your description, the VS support SFTP now. And you can upload and download files from SFTP server using SSH.NET library.
    Here is a code example you can refer to.
    Download file:

    using (SftpClient sftp = new SftpClient(host, username, password))
    {
        try
        {
            sftp.Connect();
            //MessageBox.Show("Machine 1 - Connected");
            var files = sftp.ListDirectory(remoteDirectory);
            foreach (var file in files)
            {
                try
                {
                    string remoteFileName = file.Name;
                    //if ((file.Name.EndsWith(".db")))
                    using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory, remoteFileName)))
                    {
                        string path = remoteDirectory + "/" + remoteFileName;
                        sftp.DownloadFile(path, file1);
                    }
                }
                catch (Exception er1)
                {
                    MessageBox.Show("An exception has been caught " + er1.ToString());
                }
            }
        }
        catch (Exception entry)
        {
            MessageBox.Show(entry.Message);
        }
        finally
        {
            sftp.Disconnect();
        }
    }

    Upload file:

    using (var client = new SftpClient(host, port, username, password))
    {
        client.Connect();
        if (client.IsConnected)
        {
            Console.WriteLine("I'm connected to the client");
    
            using (var fileStream = new FileStream(uploadFile, FileMode.Open))
            {
    
                client.BufferSize = 4 * 1024; // bypass Payload error large files
                client.UploadFile(fileStream, Path.GetFileName(uploadFile));
            }
        }
        else
        {
            Console.WriteLine("I couldn't connect");
        }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 24, 2020 9:53 AM

All replies

  • Hi AlanChong,
    Based on your description, the VS support SFTP now. And you can upload and download files from SFTP server using SSH.NET library.
    Here is a code example you can refer to.
    Download file:

    using (SftpClient sftp = new SftpClient(host, username, password))
    {
        try
        {
            sftp.Connect();
            //MessageBox.Show("Machine 1 - Connected");
            var files = sftp.ListDirectory(remoteDirectory);
            foreach (var file in files)
            {
                try
                {
                    string remoteFileName = file.Name;
                    //if ((file.Name.EndsWith(".db")))
                    using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory, remoteFileName)))
                    {
                        string path = remoteDirectory + "/" + remoteFileName;
                        sftp.DownloadFile(path, file1);
                    }
                }
                catch (Exception er1)
                {
                    MessageBox.Show("An exception has been caught " + er1.ToString());
                }
            }
        }
        catch (Exception entry)
        {
            MessageBox.Show(entry.Message);
        }
        finally
        {
            sftp.Disconnect();
        }
    }

    Upload file:

    using (var client = new SftpClient(host, port, username, password))
    {
        client.Connect();
        if (client.IsConnected)
        {
            Console.WriteLine("I'm connected to the client");
    
            using (var fileStream = new FileStream(uploadFile, FileMode.Open))
            {
    
                client.BufferSize = 4 * 1024; // bypass Payload error large files
                client.UploadFile(fileStream, Path.GetFileName(uploadFile));
            }
        }
        else
        {
            Console.WriteLine("I couldn't connect");
        }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 24, 2020 9:53 AM
  • Thank you. I have to deal with xml header problems first before I can upload files. I'll try that as soon as possible.
    Friday, March 27, 2020 2:34 AM