Transfer files using winSCP in C#

There are many FTP clients and libraries available on the net, but recently i was doing a project which required some files to be zipped and uploaded to a server using SFTP, also need to do some more task after uploading the files to the server. i.e renaming/deleting etc., This program is scheduled and run automatically.

I found WinSCP is the perfect tool, as its free and does the job without any complicating stuff!

Below code will show a quick demo how to use winSCP in C#.

If you want more info you can find at http://winscp.net/eng/index.php as well.

Code

First, create a new process:

Process winscp = new Process();

now, we need to pass the winscp executable filename and path as well as other arguments;

winscp.StartInfo.FileName = @ini.IniReadValue(“winSCP”, “compath”);

I am reading the values from an ini file. (read how to create and use ini files)

as I mentioned earlier, this is a schedule program which runs automatically and we need to run the task in silent mode, so initialise the below arguments.

winscp.StartInfo.Arguments = "/log=" + logname;
winscp.StartInfo.UseShellExecute = false; // creating the process directly from the exec file 
winscp.StartInfo.RedirectStandardInput = true; //reading from a standard input stream
winscp.StartInfo.RedirectStandardOutput = true; //writing to the standard output stream
winscp.StartInfo.CreateNoWindow = true; // create the process without starting a new window

in the below code, I used the try/catch to capture any errors when starting winscp process and when an error occurred, writing that to the log file. (read how to write in log files)

try
            {
                winscp.Start();
            }
            catch (Exception ex)
            {
                writeLog("from PutSFTP:  Could not run the WinSCP executable " + winscp.StartInfo.FileName + Environment.NewLine + ex.Message);
            }

now pass the values to the StandardInput:

  winscp.StandardInput.WriteLine("option batch abort");
            winscp.StandardInput.WriteLine("option confirm off");
            winscp.StandardInput.WriteLine("open sftp://" + username + ":" + password + "@" + remotehost);
            winscp.StandardInput.WriteLine("cd " + ini.IniReadValue("Remote Directory", "Download FTP Dir"));
            winscp.StandardInput.WriteLine(@"get -delete " + "/" + ini.IniReadValue("Remote Directory", "Download FTP Dir") + "/*" + ini.IniReadValue("Remote Directory", "FileType") + " " + ini.IniReadValue("Local Directory", "Inbound Dir"));
            winscp.StandardInput.Close();

because I’m running this program without any GUI, I run this in batch mode. The option batch abort will make the program in batch mode, and should use option confirm off to disable any confirmations or messages.

In the above code, I am opening the connection by passing the user/pass/host arguments and navigating to a directory to delete files.

After performing the task, getting the output and wait for the process to shutdown.

            string output = winscp.StandardOutput.ReadToEnd();
            winscp.WaitForExit();

The other tasks i.e zipping/unzipping files, log files, ini files, sending confirmation email etc., I will write in a separate post!

Have a good day!