WPF使用WinSCP实现FTP下载
wpF 使用 WinSCP 做 FTP 下载
Nuget
安装 WinSCP
当安装完成后进入安装后的目录 packages\WinSCP.5.21.6\tools
将两个编程客栈文件拷贝 Debug
调试(运行)目录下。
下面开始代码实现使用 WinSCP FTP
下载。
示例代码
1) xaml
代码如下:
<wpfdev:Windowx: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> <WrapPanelVerticalAlignment="Center" HorizontalAlignment="Center"> <TextblockName="myTextBlock"Margin="10,0"VerticalAlignment="Center"/> <wpfdev:CircularProgressBarName="myCircularProgressBar" BrushStrokeThickness="2" StrokeThickness="5" Size="20,20" BorderBrush="#42ABAC" Background="#E14D5F" Value="0"/> <ButtonStyle="{StaticResourcePrimaryButton}"Margin="10,0"Content="Download"Click="Button_Click"/> </W编程客栈rapPanel> </Grid> </wpfdev:Window>
2) cs
代码如下:
usingSystem; usingSystem.IO; usingSystem.Threading.Tasks; usingSystem.Windows; usingWinSCP; namespaceWpfApp1 { ///<summary> ///Window1.xaml的交互逻辑 ///</summary> publicpartialclassWindow1 { publicWindow1() { InitializeComponent(); } privatevoidButton_Click(objectsender,RoutedEventArgse) { myCircularProgressBar.Value=0; Task.Run(()=> { Download(); }); } boolDownload() { try { SessionOptionssessionOptions=newSessionOptions { Protocol=Protocol.Ftp, HostName="127.0.0.1", UserName="wpfdevelopers", Password="wpfdevelopers", }; stringlocalPath=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"packages"); if(Directory编程.Exists(localPath)) DeleteDirectory(localPath); stringremotePath="packages"; using(Sessionsession=newSession()) { session.FileTransferProgress+=Session_FileTransferProgress; session.Open(sessionOptions); TransferOptionstransferOptions=newTransferOptions(); transferOptions.TransferMode=TransferMode.Binary; TransferOperationResulttransferResult=session.GetFiles(remotePath,localPath,false,transferOptions); transferResult.Check(); } returntrue; } catch(Exception) { returnfalse; } } voidDeleteDirectory(stringtarget_dir) { string[]files=Dwww.devze.comirectory.GetFiles(target_dir); string[]dirs=Directory.GetDirectories(target_dir); foreach(stringfileinfiles) { File.SetAttributes编程客栈(file开发者_JS培训,FileAttributes.Normal); File.Delete(file); } foreach(stringdirindirs) { DeleteDirectory(dir); } Directory.Delete(target_dir,false); } privatevoidSession_FileTransferProgress(objectsender,FileTransferProgressEventArgse) { Dispatcher.BeginInvoke(newAction(()=> { varvalue=(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下载内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论