如何在C#中显示正在下载的文件 [英] How to show which File is Downloading in C#

查看:78
本文介绍了如何在C#中显示正在下载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Code Project,

我在Windows Form Application(C#)中为游戏制作一个自动更新程序,我想在程序中显示哪个文件正在下载进度,

我做了成功显示完成了多少下载量,并且我也成功地在Progress Bar上显示下载。

但是我无法在Label上显示当前正在下载的文件。

我的代码在这里

Hello Code Project,
I am making one auto updater for game in Windows Form Application(C#), and i want to show in program which file is on downloading progress,
I did Success to show how many % download done and also i did success to show downloading on Progress Bar.
But i am getting failed to show on Label which File is Currently Downloading.
My code is here

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   //Defines the server's update directory
   string Server = "Server URL";

   //Defines application root
   string Root = AppDomain.CurrentDomain.BaseDirectory;

   //checks client version
   string lclVersion;
   using (StreamReader reader = new StreamReader("version"))
   {
      lclVersion = reader.ReadLine();
   }
   decimal localVersion = decimal.Parse(lclVersion);

   //server's list of updates
   XDocument serverXml = XDocument.Load(@Server + "version.xml");

   //The Update Process
   foreach (XElement update in serverXml.Descendants("update"))
   {
      string version = update.Element("version").Value;
      string file = update.Element("file").Value;

      decimal serverVersion = decimal.Parse(version);

      string sUrlToReadFileFrom = Server + file;

      string sFilePathToWriteFileTo = Root + file;

      if (serverVersion > localVersion)
      {
         Uri url = new Uri(sUrlToReadFileFrom);
         System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
         System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
         response.Close();

         Int64 iSize = response.ContentLength;
         Int64 iRunningByteTotal = 0;

         using (System.Net.WebClient client = new System.Net.WebClient())
         {
            using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
            {
               using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
               {
                  int iByteSize = 0;
                  byte[] byteBuffer = new byte[iSize];
                  while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                  {
                     streamLocal.Write(byteBuffer, 0, iByteSize);
                     iRunningByteTotal += iByteSize;

                     double dIndex = (double)(iRunningByteTotal);
                     double dTotal = (double)byteBuffer.Length;
                     double dProgressPercentage = (dIndex / dTotal);
                     int iProgressPercentage = (int)(dProgressPercentage * 100);
                     backgroundWorker1.ReportProgress(iProgressPercentage);    
                  }

                  streamLocal.Close();
               }

               streamRemote.Close();
            }
         }
      }
   }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   label3.ForeColor = System.Drawing.Color.White;
   label3.Text = e.ProgressPercentage.ToString() + "%";
   progressBar1.Value = e.ProgressPercentage;
   downloadLbl.ForeColor = System.Drawing.Color.White;
   downloadLbl.Text = "Downloading Updates";
}





在此先感谢:)



Thanks in Advance :)

推荐答案

最后解决它:))

i刚刚调整了一些代码,比如这里

in while DoWork。

Finally Solve it :))
i just adjusted some codes like here
in while Progress in DoWork.
downloadUpdates.ReportProgress(iProgressPercentage,file);





和ProgressChanged的另一个更改。





and another change on ProgressChanged.

var filename = Convert.ToString(e.UserState);
label1.Text = "Downloading Update File " + filename;


我使用 WebClient.DownloadProgressChanged [ ^ ]和 DownloadDataAsync [ ^ ]到那种工作。



DownloadDataAsync开始下载,DownloadProgressChanged事件告诉我下载了多少数据,DownloadDataComplete事件通知下载完成后。使用DownloadDataAsync获得奖金,您可以取消下载。
I use WebClient.DownloadProgressChanged[^] and DownloadDataAsync [^] to that sort for work.

DownloadDataAsync starts downloading and DownloadProgressChanged events tells how much data i downloaded and DownloadDataComplete event informs when download is completed. And the bonus with DownloadDataAsync, you can cancel the download.


这篇关于如何在C#中显示正在下载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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