欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF使用WinSCP實(shí)現(xiàn)FTP下載

 更新時(shí)間:2023年01月04日 10:12:28   作者:驚鏵  
這篇文章主要為大家詳細(xì)介紹了WPF如何使用WinSCP實(shí)現(xiàn)FTP下載,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下

WPF 使用 WinSCP 做 FTP 下載

Nuget 安裝 WinSCP

當(dāng)安裝完成后進(jìn)入安裝后的目錄 packages\WinSCP.5.21.6\tools 將兩個(gè)文件拷貝 Debug 調(diào)試(運(yùn)行)目錄下。

下面開(kāi)始代碼實(shí)現(xiàn)使用 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?=?"文件已經(jīng)全部下載完成";
????????????????else
????????????????????myTextBlock.Text?=?$"正在下載文件?{System.IO.Path.GetFileName(e.FileName)}";
????????????}));

????????}
????}
}

效果圖

下載完成的文件

到此這篇關(guān)于WPF使用WinSCP實(shí)現(xiàn)FTP下載的文章就介紹到這了,更多相關(guān)WPF WinSCP實(shí)現(xiàn)FTP下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論