Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Tamir.SharpSSH上传到FTP服务器C#_C#_Sftp_Sharpssh - Fatal编程技术网

使用Tamir.SharpSSH上传到FTP服务器C#

使用Tamir.SharpSSH上传到FTP服务器C#,c#,sftp,sharpssh,C#,Sftp,Sharpssh,我能够连接到我的sftp服务器,我确信这一点,因为我得到了我服务器的文件列表,它通过了正确的列表。但我无法将文件上传到mysftp服务器中的文件夹。这是我的密码: private static void FileUploadUsingSftp(string SFTPAddress, string SFTPUserName, string SFTPPassword, string SFTPFilePath, string FileName) { Sftp

我能够连接到我的sftp服务器,我确信这一点,因为我得到了我服务器的文件列表,它通过了正确的列表。但我无法将文件上传到mysftp服务器中的文件夹。这是我的密码:

  private static void FileUploadUsingSftp(string SFTPAddress, string SFTPUserName, string SFTPPassword,
        string SFTPFilePath, string FileName)
    {
        Sftp sftp = null;
        try
        {

            sftp = new Sftp( SFTPAddress,SFTPUserName , SFTPPassword);

            // Connect Sftp
            sftp.Connect();
            MessageBox.Show("Connected!");



            //Check if im surely connected
            //list down files in my sftp server folder u01
            ArrayList list;
            list = sftp.GetFileList("//u01");

            foreach (string item in list)
            {
                MessageBox.Show(item.ToString());
            }
            MessageBox.Show(list.Count.ToString());

            // upload file 
            sftp.Put(FileName, "//u01"); -----> **I get exception here**

            MessageBox.Show("UPLOADED!");


            // Close the Sftp connection
            sftp.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            if (sftp != null)
            {
                sftp.Close();
            }
        }
    }
我收到这个例外:

"Exception of type 'Tamir.SharpSsh.jsch.SftpException' was thrown."
at Tamir.SharpSsh.jsch.ChannelSftp.put(String src, String dst, SftpProgressMonitormonitor, Int32 mode)
at Tamir.SharpSsh.Sftp.Put(String fromFilePath, String toFilePath)
我尝试过使用sftp.Put(文件名,SFTPAddress+“//u01”)

我试过sftp.Put(文件名,SFTPAddress);它确实可以工作,但当我查看我的sftp服务器时,如果文件在那里,它就不在了

我尝试了sftp.Put(文件名,//u01);它抛出了同样的错误

我必须将文件上载到ftp服务器的文件夹中,其中一个文件夹是u01


有人能帮我吗。我不知道怎么了。我确信我是有联系的。当我尝试使用filezilla上传时,它确实起作用,因此我没有被限制写入我们的sftp服务器。

我相信您必须在调用
Put
时输入完整的文件名

string strippedFileName = StripPathComponent(FileName);
sftp.Put(FileName,"//u01//" + strippedFileName);

请注意,
StripPathComponent
没有实现,如果需要,您也必须实现它。它将从
文件名
中删除路径组件,即删除
C:\…\
。\…\

我还在搜索如何使用此库将文件从本地/共享路径上载到SFTP服务器,并最终找到了解决方案。您可以使用下面的代码

string host="ssgty";
string username="usr";
string password="passw";
int port=22;
string fromFile=@"D:\Test.txt";
string toFile=@"/dmon/myfolder/Test.txt";

public string CopyToFTP(string host, string username, string password, int port, string fromFile, string toFile)
{
      string error = "";
      try
      {
           Scp scp = new Scp(host, username, password);
           scp.Connect(port);
           scp.To(fromFile, toFile);
      }
      catch (Exception ex)
      {
           error = ex.Message;
      }
      return error;
}

好吧,我只是弄错了!你伟大的@fredrik。谢谢!成功了!我使用了Path.GetFileName(FileName);