2016-02-10 95 views
0

我沒有收到錯誤,但我的文件沒有上傳。我試圖上傳到SFTP上的targetDirectory。使用Renci ssh上傳到SFTP目錄

public string TryUploads(string targetDirectory) 
{ 
    string _localDirectory = LocalDirectory; //The directory in SFTP server where the files are present 

    if (oSftp == null) 
    { 
     oSftp = Instance; 
    } 

    lock (thisLock) 
    { 
     try 
     { 
      oSftp.Connect(); 
      List<string> fileList = Directory.GetFiles(_localDirectory, "*.*").ToList<string>(); 
      oSftp.ChangeDirectory(targetDirectory); 
      if (fileList != null && fileList.Count() > 1) 
      { 

       for (int i = 0; i < fileList.Count(); i++) 
       { 


        string ftpFileName = Path.GetFileName(fileList[i]); 
        if (!String.IsNullOrEmpty(targetDirectory)) 
         ftpFileName = String.Format("{0}/{1}", targetDirectory, ftpFileName); 

        using (var stream = new FileStream(Path.GetFileName(fileList[i]), FileMode.Create)) 
        { 


         oSftp.BufferSize = 4 * 1024; 

         oSftp.UploadFile(stream, ftpFileName); 
         // stream.Close(); 

        } 


       } 


      } 
      oSftp.Disconnect(); 

     } 
     catch (Exception e) 
     { 
      throw new ApplicationException(e.Message); 
     } 

    } 
    return Strings.StatusOk; 
} 

回答

0

我解決了這個問題。我需要把從流中獲取文件的位置

using (var stream = new FileStream(LocalDirectory + "\\" + Path.GetFileName(fileList[i]), FileMode.Open)) 

已解決。上傳的文件;)