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();
}