使用Renci ssh上传到SFTP目录

时间:2016-02-10 16:26:37

标签: c# sftp

我没有收到错误但我的文件没有上传。我试图在SFTP上传到targetDirectory。

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

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我需要在流

中放置从中获取文件的位置
using (var stream = new FileStream(LocalDirectory + "\\" + Path.GetFileName(fileList[i]), FileMode.Open))

解决。上传的文件;)