WPF 使用 WinSCP 做 FTP 下载
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下载的文章就介绍到这了,更多相关WPF WinSCP实现FTP下载内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!