使用webclient从服务器进度条下载文件而不更新WPF [英] Using webclient to download file from server progress bar not updating WPF

查看:117
本文介绍了使用webclient从服务器进度条下载文件而不更新WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的自动更新程序WPF应用程序我从服务器下载文件但进度条没有更新检查下面的代码

I have a simple Auto Updater WPF application I am downloading files from Server but the progress bar is not updating check out the below code

private void Button_Click_3(object sender, RoutedEventArgs e)
{
    try
    {
        string LocalFilePath = System.IO.Directory.GetCurrentDirectory();
        LocalFilePath += @"\FishBot.exe";

        DateTime SysFileDate = File.GetLastWriteTime(LocalFilePath);

        string ftpURL = "ftp://www.intelligent-solutions.at/mrniceguy.exe";

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);
        request.Method = WebRequestMethods.Ftp.GetDateTimestamp;

        //You could add Credentials, if needed 
        request.Credentials = new NetworkCredential("philippftp2", "yijnobrgap");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        DateTime ServerFileDate = response.LastModified;

        if (SysFileDate & gt; ServerFileDate)
        {
            dashTab = false;
            fishTab = false;
            shovelTab = false;
            hpTab = false;
            ingameSettingsTab = false;
            DownloadUpdateTab = true;
            RaisePropertyChanged("DashTab");
            RaisePropertyChanged("FishTab");
            RaisePropertyChanged("ShovelTab");
            RaisePropertyChanged("HPTab");
            RaisePropertyChanged("IngameSettingTab");
            RaisePropertyChanged("DownloadUpdateTab");
            if (MessageBox.Show("New Update is Available, Click OK to download or Cancel to download it later", "Software Update Available", MessageBoxButton.OKCancel, MessageBoxImage.Information) == MessageBoxResult.OK)
            {
                DownloadUpdates();
                ProgBar = true;
                RaisePropertyChanged("ProgBar");
            }
            else
            {
                MessageBox.Show("No New Update is Available", "Software Update", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
    }
    catch (WebException er)
    {
        String status = ((FtpWebResponse)er.Response).StatusDescription;
    }
    //WebClient webClient = new WebClient();
    //webClient.DownloadFile("https://www.intelligent-solutions.at/patch/mrniceguy.exe", @"..\..\Updates\mrniceguy.exe");



    //WebClient webClient = new WebClient();
    //webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
    //webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
    //  webClient.DownloadFileAsync(new Uri("https://www.dropbox.com/sh/5w1brwtfdxdqq3c/AABv9iM5q0jcTHo3rg0GWuGfa?dl=0"), @"c:\myfile.txt");
    //MessageBox.Show("This is working");
}

private void ProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
    ProgressBar.Value = e.ProgressPercentage;
}


private void Completed(object sender, AsyncCompletedEventArgs e)
{
    MessageBox.Show("Download completed!");
}
private void DownloadUpdates()
{
    try
    {

        string ftpURL = "ftp://www.intelligent-solutions.at/";
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);
        request.Method = WebRequestMethods.Ftp.ListDirectory;

        //You could add Credentials, if needed 
        request.Credentials = new NetworkCredential("philippftp2", "yijnobrgap");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        DateTime dt = response.LastModified;
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        List & lt; string> directories = new List& lt; string> ();

        string line = reader.ReadLine();
        while (!string.IsNullOrEmpty(line))
        {
            directories.Add(line);
            line = reader.ReadLine();
        }
        reader.Close();


        //using (WebClient ftpClient = new WebClient())
        //{
        //ftpClient.Credentials = new System.Net.NetworkCredential("philippftp2", "yijnobrgap");

        for (int i = 0; i & lt;= directories.Count - 1; i++)
            {
            if (directories[i].Contains("."))
            {

                string path = "ftp://www.intelligent-solutions.at/" + directories[i].ToString();
                //Uri ur = new Uri(path);
                string trnsfrpth = @"..\..\Updates\" + directories[i].ToString();
                //ftpClient.DownloadFile(path, trnsfrpth);// (path, trnsfrpth);
                WebClient client = new WebClient();
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                client.DownloadFileAsync(new Uri(path), trnsfrpth);

            }
            else
            {
                // Directory.CreateDirectory(@"..\..\Updates\" + directories[i].ToString());
                string ftpURL1 = "ftp://www.intelligent-solutions.at/" + directories[i].ToString();
                request = (FtpWebRequest)WebRequest.Create(ftpURL1);
                request.Method = WebRequestMethods.Ftp.ListDirectory;

                //You could add Credentials, if needed 
                request.Credentials = new NetworkCredential("hhhhh", "yijnobrgap");

                response = (FtpWebResponse)request.GetResponse();

                responseStream = response.GetResponseStream();
                reader = new StreamReader(responseStream);
                List & lt; string> subdirectories = new List& lt; string> ();

                string line1 = reader.ReadLine();
                while (!string.IsNullOrEmpty(line1))
                {
                    subdirectories.Add(line1);
                    line1 = reader.ReadLine();
                }
                reader.Close();
                //using (WebClient ftpClient1 = new WebClient())
                //{
                //    ftpClient1.Credentials = new System.Net.NetworkCredential("hhhh", "yijnobrgap");

                for (int j = 0; j & lt;= subdirectories.Count - 1; j++)
                        {
                    if (subdirectories[j].Contains("."))
                    {

                        try
                        {
                            string path = "ftp://www.intelligent-solutions.at/" + subdirectories[j].ToString();
                            Uri ur = new Uri(path);
                            string trnsfrpth1 = @"..\..\Updates\" + subdirectories[j].ToString();

                            // File.SetAttributes(trnsfrpth1, FileAttributes.Normal);

                            //ftpClient1.DownloadFile(path, trnsfrpth1);
                            WebClient client = new WebClient();
                            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                            client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                            client.DownloadFileAsync(new Uri(path), trnsfrpth1);
                        }
                        catch (WebException ex)
                        {
                            //String status = ((FtpWebResponse)exx.Response).StatusDescription;
                        }

                    }
                }
                //}
            }
            //ProgressBar.Value += Math.Ceiling(Convert.ToDouble(100 / directories.Count));
        }
        // }
    }
    catch (WebException er)
    {
        String status = ((FtpWebResponse)er.Response).StatusDescription;
    }

}





我尝试过:



我尝试过使用Dispatcher但没有工作



What I have tried:

I have tried using Dispatcher but not work

推荐答案

这是WPF中的一个已知问题,这里是一篇展示如何操作的文章:

绑定介绍 [ ^ ]
This is a known problem in WPF, here is an article that shows how to do it:
introduction to binding[^]


这篇关于使用webclient从服务器进度条下载文件而不更新WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆