2016-03-29 22 views
0

FTPを使用してファイルをアップロードしました(下記のコードを参照)、正しく動作しています。しかし今はSFTPを使ってファイルをアップロードする必要があります VSでsftpに使用するライブラリを案内してくれるようにお願いします。SFTPファイルのアップロード

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); 
     } 

} 
} 

答えて

-1

あなたが使用することができますSharpSSHという名前の何かがあります:

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

使用例:

sftp = new Tamir.SharpSsh.Sftp(ipAddress, username, password); 
sftp.Connect(); 
sftp.Get(sourcePath, destinationPath); 
sftp.Close(); 
+1

SharpSSHは使用しないでください。あなたが "C#SFTP"を検索すると、多かれ少なかれ使いやすくシンプルなライブラリがたくさん出てくるでしょう(2007年以来更新されていない) –

+0

SharpSSHのように。その答えの鍵となる価値は、検索はあなたの友人です。 –

+0

サードパーティ製のライブラリを使用せずにFile upload SFTPを作成する方法はありますか? – Sandy777