Secure FTP connection

I am trying to build a component to get files from a secure Ftp server (also push it on the server) . The one and only port open onthe server is 22. I tried using FTP libraries that are recommended here, but none worked correctly. The last FTP lib. I tried is FTPFactory.cs. Please advice...
Who is Participating?
KeithWatsonConnect With a Mentor Commented:
Oh, I don't know, not been around here long... Don't worry about it though. Glad you got the problem sorted.
SFTP requires an SFTP client; the class you used looks like a standard FTP client. Unfortunately it doesn't look as though there are any free component libraries to talk to an SFTP server; however, there are a couple of commerical components available:

There are also a number of free C libraries available, which you could use. These are documented in this thread:

If it's OK to shell out to an external program to do the work, then SecureFx is very good and cheap:
web121Author Commented:
Thanks for the quick response. I am trying to find a C# library and I don't want to use commercial components.
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

web121Author Commented:
After searching for it, I couldn't find a C# library that talks to SFTP server. Now I am planning to execute a SSH client from the command line using C#.  I am using System.Diagnostics.Process to attach to a cmd.exe, any ideas how can I interact with cmd.exe using Process class to run SSH client and run ftp commands on it. SSH client I am planning to use is puTTy. How about creating batch files and executing them accordly sounds? Any alternatives??

A batch file sounds fine; you can also just fire commands into cmd.exe on the command line; so:

cmd.exe /c notepad.exe

will start a command shell, execute notepad and then once notepad is complete (you'd have to kill it in this instance, close the command shell).

You can chain commands together like this as well:

cmd.exe /c dir & pause & dir & pause

So this could save you writing a batch file, but this would also work.

web121Author Commented:
Code is as follows, it executes in a webserver as a Webservice under ASPNet account. Cmd.exe starts and exits under ASPNet account with no problem. I need to tell cmd.exe to run the batchfile.bat (that's my file name) somehow. There is no error on the batch file, if I run it manually  it copies the file and closes the cmd.exe. FTP folder has full controll for ASPNet machine account.

//objProcess.StartInfo.Arguments = "/c batchfile.bat";  ??? I tried this but did not work.!! also tried
objProcess.StartInfo.FileName = @"C:\WINDOWS\system32\cmd.exe /c batchfile.bat";


CS code
bool bIsFinished = false;
Process objProcess = new Process();

objProcess.StartInfo.FileName = @"C:\WINDOWS\system32\cmd.exe";
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardInput = true;
objProcess.StartInfo.RedirectStandardOutput = true;
objProcess.StartInfo.RedirectStandardError = true;

bIsFinished = objProcess.WaitForExit(10000);//10 seconds

ftp -s:batchfile.scr

open ftpservername
put Errorlog.log
web121Author Commented:
I am trying to get it to work with FTP first. Then I am planning on moving it to sftp.
web121Author Commented:
solved the problem for ftp

                                                System.Diagnostics.ProcessStartInfo psi =   new System.Diagnostics.ProcessStartInfo(@"C:\PSFTPbatchfile.bat");
                  psi.RedirectStandardOutput = true;
                  psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                  psi.UseShellExecute = false;
                  System.Diagnostics.Process objProcess;
                  objProcess = System.Diagnostics.Process.Start(psi);
                  System.IO.StreamReader myOutput = objProcess.StandardOutput;
                  if (objProcess.HasExited)  
                        string output = myOutput.ReadToEnd();

but same code does not work for sftp. I am using psftp (which is putty ssh comandline)
again batch file work fine when I am running it manually from cmd.exe but when it is run like above it does not copy the file, and cmd.exe does not exit if I look at the task manager. Any ideas ?? Everything is in the same directory c:\

chdir c:\
psftp ftp01 -l admin -pw adminpwd -b PSFTPbatchfile.scr

 cd home
put errorlog.log
I've never run putty via the command line; however, one thing you might try is sending the batch file is as a parameter to cmd.exe and see if that makes a difference, so instead of executing C:\PSFTPbatchfile.bat, try executing:

cmd /c call C:\PSFTPbatchfile.bat

You would need to set these in the ProcessStartInfo object separately, i.e.

...ProcessStartInfo("cmd.exe", "/c C:\\PSFTPbatchfile.bat");

Incidentally, instead of waiting 5 seconds and then checking to see if the process has exited you can call:


and it will block until completion.
web121Author Commented:
Here is the actual problem when doing sftp with ASPNET user. First time connecting to the sftp server with putty commandline it asks you to response to the message below. You can not automate the response because it is a security concern. If you say yes to the message below it cashes it to the registry and never ask you this again. On the web app that I am doing process is started from a ASPNET user, Since I can not login to the machine as an ASPNET user I can not say "yes" to the message below. Any ideas on this issue??

I tried ProcessStartInfo("cmd.exe", "/c C:\\PSFTPbatchfile.bat"); there is no difference

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's dss key fingerprint is:
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
Could it be the permissions on the website itself? The default setting for ASP.NET websites you can see by right-clicking the web application in ASP.NET, selecting properties and then the "Home Directory" tab. Towards the bottom there is a combo box with "Execute Permissions" next to it; this defaults to "Scripts Only". You could try changing this to "Scripts and Executables".

I'm afraid I know very little about the Putty SFTP client although I do remember the message you point out. I think it's just saying that you've supplied the public key but that it could have come from anywhere, so make sure you know where the public key came from, i.e. make sure you're connecting to the computer you think you are.

Hope that helps,

web121Author Commented:
I solved the problem.. :-) by using impersonation...

Change the webconfig of the application (most likely u are going to add this line) in <system.web>

<identity impersonate="true" userName="domain\username" password="pwd" />
Good stuff.

Glad you got it working.
web121Author Commented:
Keith I would like to give you some points because u were really trying to help. How can I do this? (  Not 300 but 100 ??)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.