Share via

Using sftp:// in a Project

Steve Pollere 0 Reputation points
2026-04-17T17:45:12.9166667+00:00

For starters, I am very inexperienced in Visual Studio. We have a small project that is called from an Access form that connects to and uploads files to an ftp site. It worked flawlessly for years, but then my ftp provider changed to sftp. For the life of me, I cannot get the script to connect to it. I have read that sftp is not supported out of the box, but github has a ssh.net add-on that helps. All of it was a little over my head. Below is the code that makes the connection and uploads the files. Any help is greatly appreciated.

        testFTPConnection(ftpAddress, ftpUsername, ftpPassword);

    }

    // Name:       testFTPConnection()

    // Parameters: 'string' address  - FTP address for connection

    //             'string' username - username for FTP server. Default value of 'anonymous' if none

    //                                 is provided.

    //             'string' password - password for FTP server. Default value of '' if non is provided.

    // Purpose:    Takes in address, username, and password of FTP server and attempts connection. If 

    //             connection is successful and correct file is found, the file is uploaded to the server.

    //             Error handling will alert if connection was unsuccessful. Likewise, a successful 

    //             connection will also be displayed through a message box.

    // Return:     None

    private void testFTPConnection( string address, string username = "anonymous", string password = "" )

    {

        // Create an FTP request object and set variables to appropriate values for connection

        FtpWebRequest request = ( FtpWebRequest )WebRequest.Create( new Uri( address ) );

        request.Credentials   = new NetworkCredential( username.Normalize(), password.Normalize() );

        request.UsePassive    = true;

        request.UseBinary     = false;

        request.EnableSsl     = true;                       

        request.KeepAlive     = true;

        request.Method        = WebRequestMethods.Ftp.UploadFile;

        // This is a quick and dirty way to bypass the FTP server certificate authentication

        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback( delegate { return true; } );

        try

        {

            // Find the file to be uploaded and put contents in buffer

            FileStream fs = File.OpenRead( fullPath );

            byte[] buffer = new byte[ fs.Length ];

            fs.Read( buffer, 0, buffer.Length );

            fs.Close();

            // Upload the file to the FTP server

            Stream requestStream = request.GetRequestStream();

            requestStream.Write(buffer, 0, buffer.Length);

            requestStream.Flush();

            requestStream.Close();

            // Console output for debugging purposes. Successful connection yields a messagebox

            Console.WriteLine("Connecting to " + request.RequestUri + " ...\n\n");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            MessageBox.Show("\n\nFile uploaded successfully!");

            Close();

        }

        catch (WebException ex)

        {

            MessageBox.Show("Status: " + ex.Status + "\n" + 

                            "Error:  " + ((FtpWebResponse)ex.Response).StatusDescription);

            Close();

        }

        catch (FileNotFoundException ex)

        {

            MessageBox.Show("Error: " + ex.Message);

            Close();

        }
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-04-17T17:45:28.15+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    FtpWebRequest only supports FTP and FTPS (FTP over SSL/TLS). It does not support SFTP (SSH File Transfer Protocol). Changing the URL to sftp:// and setting EnableSsl = true cannot make this work, because SFTP is a different protocol that runs over SSH, not FTP.

    Key points from the existing code:

    • FtpWebRequest is created via WebRequest.Create(address).
    • request.Method = WebRequestMethods.Ftp.UploadFile;
    • request.EnableSsl = true; enables FTPS, not SFTP.

    When the provider switched from FTP/FTPS to SFTP, the entire FtpWebRequest-based approach became incompatible. There is no property or flag on FtpWebRequest that can be changed to make it speak SFTP.

    To upload to an SFTP server from .NET, a different library that implements SFTP must be used (for example, an SSH/SFTP client library). The current code cannot be adapted to SFTP using only the .NET FtpWebRequest/FtpWebResponse APIs.

    Additionally, note that FtpWebRequest is not recommended for new development and FTP support is removed in .NET 6 and later, as documented in the FTP and FtpWebRequest references.


    References:

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.