Я работал над этим файлом xaml.Я хочу скачать целевой файл.Насколько я мог, я могу скачать файл, однако мой индикатор выполнения не работает должным образом (он остается на 0%)
Поскольку я новичок в этом языке, я не знаю всех ссылок и т. Д.Поэтому, может быть, я что-то упускаю.
Вот полный код: (У меня неиспользуемый импорт, но я удалю его позже)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Forms;
using System.Windows.Shapes;
using System.Net.Mime;
namespace DownloadingFileWindow
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
WebClient client = new WebClient();
string url = "https://download.filezilla-project.org/client/FileZilla_3.32.0_win64-setup_bundled.exe";
private void btnDownload_Click(object sender, RoutedEventArgs e)
{
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
//client.OpenRead(url);
//string header_contentDisposition = client.ResponseHeaders["content-disposition"];
//string filename = new ContentDisposition(header_contentDisposition).FileName;
if (!string.IsNullOrEmpty(url))
{
Thread thread = new Thread(() =>
{
Uri uri = new Uri(url);
string fileName = System.IO.Path.GetFileName(uri.AbsolutePath);
client.DownloadFileAsync(uri, System.Windows.Forms.Application.StartupPath + "/" + fileName);
});
thread.Start();
}
}
private void MainWindow_Load(object sender, EventArgs e)
{
client.DownloadProgressChanged += Client_DownloadProgressChanged;
client.DownloadFileCompleted += Client_DownloadFileCompleted;
}
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Download OK!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Minimum = 0;
double receive = double.Parse(e.BytesReceived.ToString());
double total = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = receive / total * 100;
lblStatus.Text = $"{string.Format("{0:0.##}", percentage)}%";
progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
}
}
}