我想用C#连接到FileZilla sftp服务器并自动下载我选择的文件到我电脑上的文件夹,我该怎么做?

问题描述 投票:0回答:1
    private void Form1_Load(object sender, EventArgs e)
    {
        String Host = "sftp...";
        //int Port = 88;
        //String RemoteFileName = "TheDataFile.txt";
        //String LocalDestinationFilename = "TheDataFile.txt";
        String Username = "login";
        String Password = "password";


        using (SftpClient sftp = new SftpClient(Host, 88, Username, Password))
        {
            try
            {
                sftp.Connect();
                if (sftp.IsConnected) //bağlantı başarılı ise buraya giriyor...
                {
                    label1.Text = "connection successful.";
                }
                else
                {
                    label1.Text = "connection unsuccessful!";
                }
            }
            catch
            {
                label1.Text = "error!";
            }
        }

        //serverdan dosyaları indirmek için yazdığım kod
        string remotePath = "sftp://login**/0001.7z";
        string localPath = "\"C:\\Users\\Halis\\Desktop\\indirdiğim\"";
        string host = "sftp...";
        string username = "login name";
        string password = "password";
        //
        using (WebClient sftpClient = new WebClient())
        {
            sftpClient.Credentials = new NetworkCredential(username, password);

            try
            {

                string sftpServer = "sftp...";
                string remoteFilePath = "sftp:/login**//0001.7z";
                string localFilePath = "\"C:\\Users\\Halis\\Desktop\\indirdiğim\"";

                sftpClient.DownloadFile($"{sftpServer}/{remoteFilePath}", localFilePath);   
               
                Console.WriteLine("Dosya indirildi.");
            }
            catch (WebException ex)
            {
                Console.WriteLine($"Hata: {ex.Message}");
            }
        }
    }
}

}

我想下载它,但我无法下载。我是否需要表单应用程序才能连接,或者是否需要添加按钮才能连接?我很困惑。 请帮助我

c# sftp filezilla
1个回答
0
投票

您无法使用

WebClient
进行 SFTP。实际上 .NET 中根本不支持本机 SFTP。

你必须使用第三方库。

参见:

© www.soinside.com 2019 - 2024. All rights reserved.