Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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;
    }

}


What I have tried:

I have tried using Dispatcher but not work
Posted
Updated 24-Mar-17 22:14pm
v2
Comments
[no name] 24-Mar-17 16:20pm    
We aren't a "debug my entire codebase for me" service. And your problem likely stems from trying to do this from inside a button click event.
Malikdanish 25-Mar-17 0:40am    
I shared a code for review

1 solution

This is a known problem in WPF, here is an article that shows how to do it:
introduction to binding[^]
 
Share this answer
 
Comments
RickZeeland 29-Mar-17 13:06pm    
And ? did you give up or loose interest ?
Malikdanish 29-Jul-17 15:09pm    
i fix the issue :) quite a long ago
RickZeeland 29-Jul-17 15:45pm    
Ah, now I can go to sleep reassured. I love Denmark btw. visited Mon and Arhus some years ago ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900