c#webclient在一个系统上下载空文件,但在另一个系统上正常工作

时间:2016-08-30 16:34:46

标签: c# asynchronous webclient

我使用“更新程序”代码创建了一个小型启动程序,以下载具有版本号的文件。更新程序根据安装程序附带的文件测试该版本号。如果更大,请下载.msi并执行。问题是在Server 2012计算机上,要下载的版本文件在到达时为空,而在Server 2012 R2上,下载的版本将填充并正确。我的爸爸的8.1机器也是如此。见代码。显然,我也没有blah.com。

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Reflection;

namespace ra2kui
{

    public partial class Updater : Form
    {
        Stopwatch sw = new Stopwatch();
        string temppath = Path.GetTempPath();

        public Updater()
        {
            InitializeComponent();
            labelAssemblyVersion.Text = String.Format("Assembly Version: {0}", AssemblyVersion);
        }
        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        private void MainPage_Load(object sender, EventArgs e)
        {
            string currentver;

            if (File.Exists(@"lib\\vo.r2k"))
            {
                using (StreamReader currentverreader = new StreamReader("lib\\vo.r2k"))
                {
                    currentver = currentverreader.ReadLine() ?? "";

                }
                labelCurrentVersion.Text = "Current Package Version: " + currentver;
            }
            else
            {
                MessageBox.Show("VO missing.");
                return;
            }

            buttonMulti.Text = "Downloading Version File From Server...";
            labelVersionOnServer.Text = "Server Package Version: ";
            if (File.Exists(temppath + @"2kuv.r2k"))
            {
                File.Delete(temppath + @"2kuv.r2k");
            }

            Application.DoEvents();

            // Download the version from the server...
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

            // Start the stopwatch
            //Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
               webClient.DownloadFileAsync(new Uri("https://www.blah.com/package/2kuv.r2k"), temppath + @"2kuv.r2k");


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {

            // Progress bar value change
            progressBar1.Value = e.ProgressPercentage;

            // Calculate download speed and output it to labelSpeed.
            labelSpeed.Text = string.Format("{0} kB/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0"));

            // Show the percentage on our label.
            labelPerc.Text = e.ProgressPercentage.ToString() + "%";

            // Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
            labelDownloaded.Text = string.Format("{0} MB's / {1} MB's",
                (e.BytesReceived / 1024d / 1024d).ToString("0.00"),
                (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));

        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            string updatever;
            string origver;

            using (StreamReader updatereader = new StreamReader(temppath + "2kuv.r2k"))
            {
                updatever = updatereader.ReadLine() ?? "";
            }
            using (StreamReader origreader = new StreamReader("lib\\vo.r2k"))
            {
                origver = origreader.ReadLine() ?? "";
            }
            if (String.Compare(origver,updatever) > 0)
            {
                MessageBox.Show("There is no update available. Or there was an issue with the UV file.");
                this.Close();
            }
            else if(String.Compare(origver,updatever) < 0)
            {
                MessageBox.Show("There is an update available.");
                labelVersionOnServer.Text = "Server Package Version: " + updatever;
                buttonMulti.Text = "Download and proceed with update...";
                buttonMulti.Enabled = true;
            }
            else if(String.Compare(origver,updatever) == 0)
            {
                MessageBox.Show("You are on the most current version.");
                this.Close();
            }
        }

        private void buttonMulti_Click(object sender, EventArgs e)
        {
            buttonMulti.Text = "Downloading update...";
            buttonMulti.Enabled = false;
            Application.DoEvents();

            //Create Stopwatch
            sw.Start();
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed2);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
            try
            {
                webClient.DownloadFileAsync(new Uri("https://www.blah.com/package/setup32.msi"), temppath + @"setup32.msi");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Completed2(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("The updater will now close and start the update.");
            try
            {
                Process.Start(temppath + "setup32.msi");
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Application.Exit();
            }
        }
     }
}

0 个答案:

没有答案