Use SFTP instead of FTP in older version of C# (2010 Express) - c#

I have to change an older C# script (created back in 2010 in C# Express) to use SFTP instead of FTP. It took some doing to get this script out of an old repository - and to download a version of C# 2010 Express to get the script open! But I finally managed it. I have never coded in C# so please be kind. We have several off-site PCs that need to transfer files to and from the server on their network. This script is a .dll which is referenced in several other programs on these systems, so I'll need to change the calls to this .dll in those scripts as well. I have researched using SFTP in C# and from what I've seen thought it was best to install Renci.SshNet, so that has been installed as a reference in the project. What I'm looking to do is change the function below to use SFTP instead of FTP, but I'm not really sure how to even begin -
public TimeSpan FTPTo(string strFTPServer, string strLocalFileandPath, string strFTPFileandPath, string strFTPUser, string strFTPPassword)
{
try
{
DateTime dtStart = DateTime.Now;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(strFTPServer + "/" + strFTPFileandPath);
ftp.Proxy = null;
ftp.Credentials = new NetworkCredential(strFTPUser, strFTPPassword);
//userid and password for the ftp server to given
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(strLocalFileandPath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
return DateTime.Now - dtStart;
}
catch (Exception ex1)
{
throw ex1;
}
}
Any suggestions/help would be greatly appreciated.

To upload a file to an SFTP server using SSH.NET, use SftpClient.UploadFile.
A trivial example is like:
var client = new SftpClient("example.com", "username", "password");
client.Connect();
using (var sourceStream = File.Open(#"C:\local\path\file.ext"))
{
client.UploadFile(sourceStream, "/remote/path/file.ext");
}
There's no relation to your original code. The API is completely different. You basically have to start from the scratch.

Related

Uploading files to ftp folder having special characters in name

I am trying to upload a file on FTP folder, but getting the following error.
The remote server returned an error: (550) File unavailable (e.g.,
file not found, no access)
I am using the following sample to test this:
// Get the object used to communicate with the server.
string path = HttpUtility.UrlEncode("ftp://host:port//01-03-2017/John, Doe S. M.D/file.wav");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("user", "password");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(#"localpath\example.wav");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
I am able to upload files on the parent folder 01-03-2017 but not in the target folder ROLLINS, SETH S. M.D which clearly has special characters in it.
I am able to upload files using FileZilla
I have tried to HttpUtility.UrlEncode but that did n't help
Thanks for your time and help.
You need to encode the spaces (and maybe commas) in the URL path, like:
string path =
"ftp://host:port/01-03-2017/" +
HttpUtility.UrlEncode("John, Doe S. M.D") + "/file.wav";
Effectively, you get:
ftp://host:port/01-03-2017/John%2c+Doe+S.+M.D/file.wav
Use something like this:
string path = HttpUtility.UrlEncode("ftp://96.31.95.118:2121//01-03-2017//ROLLINS, SETH S. M.D//30542_3117.wav");
or You can form a Uri using the following code and pass it webrequest.
var path = new Uri("ftp://96.31.95.118:2121//01-03-2017//ROLLINS, SETH S. M.D//30542_3117.wav");
The code works on a C# console application but did not work in Web Api Action. I could not manage to find the reason.
So I have used a free library for the same.
Posting the sample code from one of the examples available here:
So i have used FluentFtp libary available through Nuget.
using System;
using System.IO;
using System.Net;
using FluentFTP;
namespace Examples {
public class OpenWriteExample {
public static void OpenWrite() {
using (FtpClient conn = new FtpClient()) {
conn.Host = "localhost";
conn.Credentials = new NetworkCredential("ftptest", "ftptest");
using (Stream ostream = conn.OpenWrite("01-03-2017/John, Doe S. M.D/file.wav")) {
try {
// istream.Position is incremented accordingly to the writes you perform
}
finally {
ostream.Close();
}
}
}
}
}
}
Again, if the file is a binary file, StreamReader should not be used as explained here.

c# / More effective way to upload file with ftp

I'm uploading a .txt file with c# and:
client.Credentials = new NetworkCredential(ftpU, ftpP);
client.UploadFile("here ftp server", "STOR", lfilepath);
And sometimes it just throws error like "System Error"
This .txt is just log in information, content of this txt is like User: Name At: 2015/12/12 08:43 AM
Is there any option to eliminate this error? Make ftp upload more effective? Or any idea to save log on informations in Internet.
Try to use FtpWebRequest and WebRequestMethods.Ftp.UploadFile. Here is the piece of code which we use for uplaoding files from ZipArchive to FTP (so there is also an option showing creating a directories if you need it too). From all methods which I've tested it was the most efficient one.
//// Get the object used to communicate with the server.
var request =
(FtpWebRequest)
WebRequest.Create("ftp://" + ftpServer + #"/" + remotePath + #"/" +
entry.FullName.TrimEnd('/'));
//// Determine if we are transferring file or directory
if (string.IsNullOrWhiteSpace(entry.Name) && !string.IsNullOrWhiteSpace(entry.FullName))
request.Method = WebRequestMethods.Ftp.MakeDirectory;
else
request.Method = WebRequestMethods.Ftp.UploadFile;
//// Try to transfer file
try
{
//// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(user, password);
switch (request.Method)
{
case WebRequestMethods.Ftp.MakeDirectory:
break;
case WebRequestMethods.Ftp.UploadFile:
var buffer = new byte[8192];
using (var rs = request.GetRequestStream())
{
StreamUtils.Copy(entry.Open(), rs, buffer);
}
break;
}
}
catch (Exception ex)
{
//// Handle it!
LogHelper.Error<FtpHelper>("Could not extract file from package.", ex);
}
finally
{
//// Get the response from the FTP server.
var response = (FtpWebResponse) request.GetResponse();
//// Close the connection = Happy a FTP server.
response.Close();
}

When I use the .NET WebClient DownloadFileAsync I randomly get zero length files returned

I'm trying to download files from my FTP server - multiples at the same time. When i use the DownloadFileAsync .. random files are returned with a byte[] Length of 0. I can 100% confirm the file exists on the server and has content AND there FTP server (running Filezilla Server) isn't erroring and say's the file has been transferred.
private async Task<IList<FtpDataResult>> DownloadFileAsync(FtpFileName ftpFileName)
{
var address = new Uri(string.Format("ftp://{0}{1}", _server, ftpFileName.FullName));
var webClient = new WebClient
{
Credentials = new NetworkCredential(_username, _password)
};
var bytes = await webClient.DownloadDataTaskAsync(address);
using (var stream = new MemoryStream(bytes))
{
// extract the stream data (either files in a zip OR a file);
return result;
}
}
When I try this code, it's slower (of course) but all the files have content.
private async Task<IList<FtpDataResult>> DownloadFileAsync(FtpFileName ftpFileName)
{
var address = new Uri(string.Format("ftp://{0}{1}", _server, ftpFileName.FullName));
var webClient = new WebClient
{
Credentials = new NetworkCredential(_username, _password)
};
// NOTICE: I've removed the AWAIT and a different method.
var bytes = webClient.DownloadData(address);
using (var stream = new MemoryStream(bytes))
{
// extract the stream data (either files in a zip OR a file);
return result;
}
}
Can anyone see what I'm doing wrong, please? Why would the DownloadFileAsync be randomly returning zero bytes?
Try out FtpWebRequest/FtpWebResponse classes. You have more available to you for debugging purposes.
FtpWebRequest - http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest(v=vs.110).aspx
FtpWebResponse - http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse(v=vs.110).aspx
Take a look at http://netftp.codeplex.com/. It appears as though almost all methods implement IAsyncResult. There isn't much documentation on how to get started, but I would assume that it is similar to the synchronous FTP classes from the .NET framework. You can install the nuget package here: https://www.nuget.org/packages/System.Net.FtpClient/

Folder & Files Upload through ftp C#

i'm trying to learn something new about uploading through ftp connection to another host.
i know how to upload single file. but what if i want to upload full folder with its full subfolders and files that exists on it?
this is my one single file upload
private void Form1_Load(object sender, EventArgs e)
{
Upload("Test.txt");
}
public void Upload(string fileToUpload)
{
try
{
FileInfo toUpload = new FileInfo(fileToUpload);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/" + toUpload.Name);
MessageBox.Show(WebRequestMethods.Ftp.ListDirectory);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("Uploader", "3635451");
Stream ftpStream = request.GetRequestStream();
FileStream file = File.OpenRead(fileToUpload);
int length = 1024;
byte[] buffer = new byte[length];
int bytesRead = 0;
do
{
bytesRead = file.Read(buffer, 0, length);
ftpStream.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
file.Close();
ftpStream.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
i google'd alot about uploading folder but i have just failed
thank you in advance.
You could use the WinSCP library. It is free, and supports FTP as well as SFTP. I have found it easy to use, and works flawlessly.
The PutFiles method will process an entire folder, including sub-folders. It also has a SynchronizeDirectories method.
The answer is you cannot.
You may be interested by libraries for doing such operations, if not you can look at their source code.
If you want to upload a folder you have to create the folder on your FTP and then copy each file one by one.
Example of a folder on local:
/Folder/File1.txt
/Folder/File2.txt
If you want to upload the folder "Folder".
Create directory in FTP
Open the FTP directory
Copy "File1.txt"
Copy "File2.txt"

FTP download not writing the data in the file

I have written a program that downloads a file from a remote ftp site and saves it to the local c drive then uploads that file to a separate server. The problem now is that when the file gets downloaded, there is no data inside the text file it creates on the local C and I can't figure out why that is. Here is the code I'm using
// Download File
public void download(string remoteFile, string localFile)
{
try
{
// Create an FTP Request
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(downhost + "/" + remoteFile);
// Log in to the FTP Server with the User Name and Password Provided
ftpRequest.Credentials = new NetworkCredential(downuser, downpass);
// When in doubt, use these options
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Set HTTP Proxy to Null to avoid The Requested FTP Command Is Not Supported When Using HTTP Proxy error */
ftpRequest.Proxy = null;
// Specify the Type of FTP Request
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
// Establish Return Communication with the FTP Server
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
// Get the FTP Server's Response Stream
ftpStream = ftpResponse.GetResponseStream();
// Open a File Stream to Write the Downloaded File
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
// Buffer for the Downloaded Data
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
// Download the File by Writing the Buffered Data Until the Transfer is Complete
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
// Resource Cleanup
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
I used the code found at http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class as the basis to build my program off of and I've done Google searches on how other people have written their ftp download scripts but can't figure out any reason why the data isn't being written.
Any help is appreciated.
Flush the file. Call localFileStream.Flush(); before you close it.
I realise that this is a bit late, but i was having the same issue trying to use that example, i managed to get a FTP download working with an example from here:
http://www.techrepublic.com/blog/howdoi/how-do-i-use-c-to-upload-and-download-files-from-an-ftp-server/165
I'm having issues with the same method. I did find a way to get the system to work but I had to copy the ftpstream in your code to another stream like this: (in vb.net, I know, I'm sorry)
ftpStream.copyTo(MySecondStream)
ftpStream.close()
'do whatever you want with the copy now
Return MySecondStream
Maybe it has something to do with how the initial stream is handled and how long it stays open. I posted my question here: Why do I have to copy an FTP stream to another variable to return it to the calling method?
I had the same problem on an upload. 0 bytes written. Code was working on one FTP server, but not on another. Just had to do the following:
client.FtpStream.Flush();
client.FtpStream.Close();

Categories