如何用WPF结合WinSCP工具实现FTP文件的批量下载操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计625个文字,预计阅读时间需要3分钟。
使用WPF结合WinSCP进行FTP下载Nuget包,安装WinSCP后,进入安装目录`packages\WinSCP.5.21.6\tools`,复制两个文件到Debug调试目录。以下代码实现使用WinSCP下载FTP文件:
csharpusing System;using WinSCP;
public class FtpDownloader{ public static void Main() { string hostName=ftp.example.com; string userName=user; string password=password; string localPath=@C:\path\to\local\file;
using (Session session=new Session()) { session.ConnectionInfo=new ConnectionInfo(hostName, userName, password); session.Open();
TransferOptions transferOptions=new TransferOptions(); transferOptions.TransferMode=TransferMode.Binary;
session.GetFiles(path/to/file, localPath, false, transferOptions).Check();
session.Close(); }
Console.WriteLine(下载完成!); }}
WPF 使用 WinSCP 做 FTP 下载
Nuget安装WinSCP
当安装完成后进入安装后的目录packages\WinSCP.5.21.6\tools将两个文件拷贝Debug调试(运行)目录下。
下面开始代码实现使用WinSCP FTP下载。
示例代码
1)xaml代码如下:
<wpfdev:Window x:Class="WpfApp1.Window1" xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft.com/winfx/2006/xaml" xmlns:d="schemas.microsoft.com/expression/blend/2008" xmlns:mc="schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wpfdev="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下载的文章就介绍到这了,更多相关WPF WinSCP实现FTP下载内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
本文共计625个文字,预计阅读时间需要3分钟。
使用WPF结合WinSCP进行FTP下载Nuget包,安装WinSCP后,进入安装目录`packages\WinSCP.5.21.6\tools`,复制两个文件到Debug调试目录。以下代码实现使用WinSCP下载FTP文件:
csharpusing System;using WinSCP;
public class FtpDownloader{ public static void Main() { string hostName=ftp.example.com; string userName=user; string password=password; string localPath=@C:\path\to\local\file;
using (Session session=new Session()) { session.ConnectionInfo=new ConnectionInfo(hostName, userName, password); session.Open();
TransferOptions transferOptions=new TransferOptions(); transferOptions.TransferMode=TransferMode.Binary;
session.GetFiles(path/to/file, localPath, false, transferOptions).Check();
session.Close(); }
Console.WriteLine(下载完成!); }}
WPF 使用 WinSCP 做 FTP 下载
Nuget安装WinSCP
当安装完成后进入安装后的目录packages\WinSCP.5.21.6\tools将两个文件拷贝Debug调试(运行)目录下。
下面开始代码实现使用WinSCP FTP下载。
示例代码
1)xaml代码如下:
<wpfdev:Window x:Class="WpfApp1.Window1" xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft.com/winfx/2006/xaml" xmlns:d="schemas.microsoft.com/expression/blend/2008" xmlns:mc="schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wpfdev="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下载的文章就介绍到这了,更多相关WPF WinSCP实现FTP下载内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

