Background:
I have developed a Windows Form-type desktop application in C# using Visual Studio Community 2015, which queries certain information from an Access database and then saves said information to a server through sftp.
Problem:
I am using the following code 1 to save the file (in this case using ftp ) which is working:
try
{
// Credenciales de prueba:
string rutaArchivo = @"C:/MiArchivo.csv";
string FTP_server = "N/A";
string FTP_username = "N/A";
string FTP_password = "N/A";
using (StreamReader stream = new StreamReader(rutaArchivo))
{
byte[] buffer = System.Text.Encoding.Default.GetBytes(stream.ReadToEnd());
WebRequest request = WebRequest.Create(FTP_server + nombreArch);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(FTP_username, FTP_password);
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("No se guardó el archivo: " + ex.Message);
}
But, when using sftp it gives me the following error:
File not saved: System.NotSupportedException: URI prefix not recognized. at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase) at System.Net.WebRequest.Create(String requestUriString)
According to the following source :
SFTP - not supported by .NET out of the box. You'll need a third party support.
I have looked at several alternatives, such as:
- WinSCP for which I'm reading the documentation, but I'm not familiar with this component (I'm checking other sources for guidance on how I can use this component in my project) .
- Renci.SshNet and SSH.NET which redirect me to the same page .
Also, to install, you would need to download it via the NuGet Package Manager for Visual Studio 2015 , but feedback from other users shows that the app is unstable.
I am looking for the "easiest" way to save a file using sftp and/or a "step by step - tutorial" on how I can download any of these components to implement in this project.
1 Modified code from the following source .
The following code uses the library
Renci SSH.Net
which is easy to implement:The method call
UploadSFTPFile
:The example code is extracted from the CodeProject blog
In the following link you can find different clients to connect via
SFTP
.Net, some of them are paid.