当我尝试从服务器下载文件时,它可以正常工作,但文件不完整。我加入一些图片,只看一下每个文件的大小。
我在这里错过了什么吗?
下载(" * // XXX / upv /"," loader.swf");
下载(" *:// 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);
}
}
}
}
答案 0 :(得分:1)
这就是我所看到的:
尝试将Download()
调用移至Window Loaded事件。您可能还需要在调度Dispatcher时包装progressBar.Value = e.ProgressPercentage
行,以确保它在UI线程上运行。见Using the C# Dispatcher
答案 1 :(得分:1)
您指定了文件夹的名称而不是文件:
Download("http://localhost/upv/", "loader.swf");
请改为尝试:
Download("http://localhost/upv/loader.swf", "loader.swf");