0

I have created file Upload using FTP (code given below) and it is working Correctly. But now I have to do a file Upload Using SFTP Kindly request you to guide me on the library to be used for sftp in VS.

class Program
{
    static void Main(string[] args)
    {

        try
        {




            string path = ConfigurationManager.AppSettings["filepath"];
            string[] files = Directory.GetFiles(path, @"*.xlsx");
            string FtpServer = ConfigurationManager.AppSettings["ftpurl"];
            string Username = ConfigurationManager.AppSettings["username"];
            string Password = ConfigurationManager.AppSettings["password"];
            string directory = ConfigurationManager.AppSettings["directoryname"];


            if (files != null)
            {
                foreach (string file in files)
                {

                    int port = 22;
                    FileInfo fi = new FileInfo(file);
                    string fileName = fi.Name;
                    string fileurl = path + @"/" + fileName;
                    string ftpFile = FtpServer + @":" + port + @"/" + directory + @"/" + fileName;
                    FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile);

                    //WebRequest myreq = (WebRequest)WebRequest.Create(ftpFile);

                    //myreq.Credentials = new NetworkCredential(Username, Password);
                    myRequest.Credentials = new NetworkCredential(Username, Password);
                    //WebProxy wb = new WebProxy("http://proxy4.wipro.com:8080");
                    //wb.Credentials = new NetworkCredential(Username, Password);
                    //myRequest.Proxy = wb;

                    myRequest.Method = WebRequestMethods.Ftp.UploadFile;
                    myRequest.Timeout = 1000000;
                    myRequest.UseBinary = true;
                    myRequest.KeepAlive = true;


                    myRequest.ContentLength = fi.Length;

                    byte[] buffer = new byte[4097];
                    int bytes = 0;
                    int total_bytes = (int)fi.Length;
                    System.IO.FileStream fs = fi.OpenRead();
                    System.IO.Stream rs = myRequest.GetRequestStream();


                    while (total_bytes > 0)
                    {
                        bytes = fs.Read(buffer, 0, buffer.Length);
                        rs.Write(buffer, 0, bytes);
                        total_bytes = total_bytes - bytes;
                    }

                    fs.Close();
                    rs.Close();

                    //WebRequest upload = (WebRequest)myreq.GetResponse();
                    FtpWebResponse uploadResponse = (FtpWebResponse)myRequest.GetResponse();
                    Console.WriteLine("Upload File Successful");
                    uploadResponse.Close();

                }
            }
        }
        catch (Exception ex)
        {
           FTPDirectory.logfile.LogInfo("Error in Uploading file in ftp://ftp.xactlycorp.com" + ex.Message);
        }

}
}
Sandy777
  • 41
  • 2
  • 8

2 Answers2

0

You can also use http://www.chilkatsoft.com/ssh-sftp-dotnet.asp

Ive used this a lot of stuff and its really good.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
-1

There is something named SharpSSH that you can use:

http://www.tamirgal.com/blog/page/SharpSSH.aspx

Example usage:

sftp = new Tamir.SharpSsh.Sftp(ipAddress, username, password);
sftp.Connect();
sftp.Get(sourcePath, destinationPath);
sftp.Close();
Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30
  • 1
    Do not use SharpSSH. It's a dead project (not updated since 2007) – Martin Prikryl Mar 29 '16 at 07:05
  • Oops, there you go - don't use it - however, if you search for "C# SFTP" you will get tons of libraries that are easy and simple to use, more or less just like SharpSSH. The key value of the answer is: Search is your friend. – Pedro G. Dias Mar 29 '16 at 07:07
  • Is there any way i can create File upload SFTP without using any 3rd party libraries? – Sandy777 Mar 30 '16 at 09:43