0

When I try to download files from server, it works but the file is incomplete. I join some images, just look at the size of each files.

Did I miss something here ?

Download("*//XXX/upv/", "loader.swf");

Download("*://XXX/upv/modules/", "modules/core.swf");

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using System.ComponentModel;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            btnPlay.IsEnabled = false; 

            if (VerifyVersion())
                btnPlay.IsEnabled = true;
            else

            Download("http://localhost/upv/", "loader.swf");
            Download("http://localhost/upv/modules/", "modules/core.swf"); 

        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("Game.exe");
        }

        private void ProgressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {

        }

        private string VersionActuelle;

                private bool VerifyVersion()
        {
            StreamReader VersionReader = new StreamReader("upv/version.txt");
            string VersionClient = VersionReader.ReadToEnd();

            VersionClient = VersionClient.Replace(System.Environment.NewLine, string.Empty);

            VersionActuelle = ReadRemoteTextFile("http://localhost/upv/version.txt");

            VersionActuelle = VersionActuelle.Replace(System.Environment.NewLine, string.Empty);

            if (VersionActuelle == VersionClient)
                return true;
            else
                return false;
        }

        private string ReadRemoteTextFile(string Url)
        {
            Uri uri = new Uri(Url); 

            WebRequest wRequest = WebRequest.Create(uri); 

            WebResponse wResponse = wRequest.GetResponse(); 

            Stream ResponseStream = wResponse.GetResponseStream(); 

            StreamReader sReader = new StreamReader(ResponseStream); 

            string Temp = sReader.ReadToEnd();

            return Temp;
        }

        private void Download(string Url, string DownloadTo)
        {
            WebClient wClient = new WebClient(); 


            wClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(UpdateProgressChange);  


            wClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(UpdateDone); 


            wClient.DownloadFileAsync(new Uri(Url), DownloadTo); 


        }

        private void UpdateProgressChange(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage; 
        }

        private void UpdateDone(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //MessageBox.Show("Mise à jour terminée! Vous pouvez désormais jouer.", "Notification");
            btnPlay.IsEnabled = true;

            File.Delete("upv/version.txt");

            using(System.IO.TextWriter sWriter = File.CreateText("upv/version.txt"))
            {
                sWriter.Write(VersionActuelle);
            }
        }
    }
}

SWF file from the server

SWF file when downloaded

Community
  • 1
  • 1
Dominic
  • 13
  • 2
  • Try https://stackoverflow.com/questions/525364/how-to-download-a-file-from-a-website-in-c-sharp – Kevin Jul 15 '17 at 06:12
  • 1
    Open the downloaded file with an editor like notepad++ and check the content. I guess you will be surprised because the answer is in there ;o) – Sir Rufo Jul 15 '17 at 06:28

2 Answers2

1

Here's what I'm seeing:

  • The download is running async so you can show progress
  • The download starts in the constructor of the Window (before the UI is Loaded)
  • The download will potentially try to update the progress bar before it is ready to receive updates
  • The web client's thread will probably crash when trying to update the uninitialized progress indicator, stopping the download.

Try moving the Download() call to the Window Loaded event. You may also need to wrap progressBar.Value = e.ProgressPercentage line in a call to the Dispatcher to make sure it runs on the UI thread. See Using the C# Dispatcher

Rob Hill
  • 371
  • 1
  • 7
1

You specified the name of the folder instead of the file:

Download("http://localhost/upv/", "loader.swf");

Try this instead:

Download("http://localhost/upv/loader.swf", "loader.swf");
elirandav
  • 1,913
  • 19
  • 27
  • it work thank you! But if I have multiple files to download from a single folder to another, do I need to specify each one in my program? – Dominic Jul 16 '17 at 00:15
  • If the server has no "mindreader" extension (I never heard of it until today), yes you have to tell exactly which file you want to download from the server – Sir Rufo Jul 16 '17 at 00:24