在C#中使用SFTP上传文件 [英] To upload file using SFTP in C#

查看:483
本文介绍了在C#中使用SFTP上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已创建用于将文件上传到SFTP远程服务器的代码,但它显示错误为

不支持给定路径的格式。



i已经提供了我的代码和示例详细信息。我确定iam给出的路径不正确,请纠正我。



我对SFTP的dll是:

使用Renci.SshNet;

使用Renci.SshNet.Sftp;



样本数据:

服务器:192.168.1.1

remotemachine(fileZilla LocalPath):C:\Program Files \ JSCAPE MFT Server \ usersrs \ behind\scope



I have created code for uploading file to SFTP remote server, But it shows error as
"The given path's format is not supported."

i have given my code and sample details. Iam sure path which iam giving is incorrect, Kindly rectify me.

my dll for SFTP is:
using Renci.SshNet;
using Renci.SshNet.Sftp;

sample data:
server: 192.168.1.1
remotemachine(fileZilla LocalPath):"C:\Program Files\JSCAPE MFT Server\users\hind\scope"

public Form1()
       {
           InitializeComponent();

           source = @"E:\To_Upload\tests_example.xls";//

       destination = "sftp://scope@hind/" + @" C:\Program Files\JSCAPE MFT Server\users\hind\scope";
           host = "hind";
           username = "scope";
           password = "S@ssw0rd987";
           port = 22;
         UploadSFTPFile(host, username, password, source, destination, port);
       }


public static void UploadSFTPFile(string host, string username, string password, string sourcefile, string destinationpath, int port)
       {
           try
           {


               using (SftpClient client = new SftpClient(host, port, username, password))
               {
                   client.Connect();
                   client.ChangeDirectory(destinationpath);
                   using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
                   {
                       client.BufferSize = 4 * 1024;
                       client.UploadFile(fs, Path.GetFileName(sourcefile));
                   }
               }
           }
           catch (Exception ex)
           {

               MessageBox.Show(ex.StackTrace, "UploadSFTPFile");
           }
       }





请妥善解决我的问题。以及如何在目的地传递SFTP路径



我尝试过:





Kindly rectify my problem. and how to pass SFTP path in destination

What I have tried:

public Form1()
        {
            InitializeComponent();

            source = @"E:\To_Upload\tests_example.xls";
         
        destination = "sftp://scope@hind/" + @" C:\Program Files\JSCAPE MFT Server\users\hind\scope";
            host = "hind";
            username = "scope";
            password = "S@ssw0rd987";
            port = 22;  
          UploadSFTPFile(host, username, password, source, destination, port);
        }


 public static void UploadSFTPFile(string host, string username, string password, string sourcefile, string destinationpath, int port)
        {
            try
            {


                using (SftpClient client = new SftpClient(host, port, username, password))
                {
                    client.Connect();
                    client.ChangeDirectory(destinationpath);
                    using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
                    {
                        client.BufferSize = 4 * 1024;
                        client.UploadFile(fs, Path.GetFileName(sourcefile));
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.StackTrace, "UploadSFTPFile");
            }
        }

推荐答案

信息清晰。



您无法在服务器上指定所需的任何文件夹路径。您只能指定位于服务器home目录下的路径。这不能以驱动器号开头。如果您只指定文件名,它将显示在服务器home文件夹的根目录中。



您无权访问任意路径您想要的服务器,只有服务器提供的服务器。
The message is clear.

You cannot specify any folder path you want on the server. You can only specify paths that sit under the "home" directory of the server. This cannot start with a drive letter. If you specify only a filename, it'll show up in the root of the "home" folder of the server.

You have no access to any arbitrary path on the server you want, only what is made available by the server.


这篇关于在C#中使用SFTP上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆