WPF 使用 WinSCP 做 FTP 下载

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 使用 WinSCP 做 FTP 下载相关的知识,希望对你有一定的参考价值。

 WPF 使用 WinSCP 做 FTP 下载

WinSCP

作   者:WPFDevelopersOrg - 驚鏵

  • Nuget 安装 WinSCP

  • 当安装完成后进入安装后的目录 packages\\WinSCP.5.21.6\\tools 将两个文件拷贝 Debug 调试(运行)目录下。

  • 下面开始代码实现使用 WinSCP FTP 下载。

1) xaml 代码如下:

<wpfdev:Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d" WindowStyle="ToolWindow"
        Title="WinSCP - FTP" Height="450" Width="800">
    <Grid>
        <WrapPanel VerticalAlignment="Center"
                   HorizontalAlignment="Center">
            <TextBlock Name="myTextBlock" Margin="10,0" VerticalAlignment="Center"/>
            <wpfdev:CircularProgressBar Name="myCircularProgressBar" 
                                        BrushStrokeThickness="2"
                                        StrokeThickness="5"
                                        Size="20,20"
                                        BorderBrush="#42ABAC" 
                                        Background="#E14D5F"
                                        Value="0"/>
            <Button Style="StaticResource PrimaryButton" Margin="10,0" Content="Download" Click="Button_Click"/>
        </WrapPanel>
    </Grid>
</wpfdev:Window>

2) cs 代码如下:

using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using WinSCP;

namespace WpfApp1

    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1
    
        public Window1()
        
            InitializeComponent();
        

        private void Button_Click(object sender, RoutedEventArgs e)
        
            myCircularProgressBar.Value = 0;
            Task.Run(() =>
            
                Download();
            );
        
        bool Download()
        
            try
            
                
                SessionOptions sessionOptions = new SessionOptions
                
                    Protocol = Protocol.Ftp,
                    HostName = "127.0.0.1",
                    UserName = "wpfdevelopers",
                    Password = "wpfdevelopers",

                ;
                string localPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"packages");
                if (Directory.Exists(localPath))
                    DeleteDirectory(localPath);
                string remotePath = "packages";
                using (Session session = new Session())
                
                    session.FileTransferProgress += Session_FileTransferProgress;
                    session.Open(sessionOptions);
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
                    TransferOperationResult transferResult = session.GetFiles(remotePath, localPath, false, transferOptions);
                    transferResult.Check();
                
                return true;
            
            catch (Exception)
            
                return false;
            
        
        void DeleteDirectory(string target_dir)
        
            string[] files = Directory.GetFiles(target_dir);
            string[] dirs = Directory.GetDirectories(target_dir);
            foreach (string file in files)
            
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            
            foreach (string dir in dirs)
            
                DeleteDirectory(dir);
            
            Directory.Delete(target_dir, false);
        
        private void Session_FileTransferProgress(object sender, FileTransferProgressEventArgs e)
        
            Dispatcher.BeginInvoke(new Action(() =>
            
               
                var value = (int)(e.OverallProgress * 100);
                myCircularProgressBar.Value = value;
                if (value == 100)
                    myTextBlock.Text = "文件已经全部下载完成";
                else
                    myTextBlock.Text = $"正在下载文件 System.IO.Path.GetFileName(e.FileName)";
            ));

        
    
  • 下载完成的文件

以上是关于WPF 使用 WinSCP 做 FTP 下载的主要内容,如果未能解决你的问题,请参考以下文章

使用 WinSCP .NET 程序集防止通过 FTP 传输不完整的文件

WinSCP中文版FTP工具 V5.19.5

winscp左边设置服务器比对

winscp读取远程目录时间长

转: window中使用PSFTP/WinSCP实现SFTP上传下载

WinSCP .NET 程序集无法识别 Session.PutFiles 中的 FTP 链接/URL