Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
C#
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 :)
Posted
Updated 13-Aug-14 4:18am
v3
Comments
Herman<T>.Instance 11-Aug-14 5:28am    
what if you create a String object in the class, fill it in DoWork and show it in ProgressChanged?
UttamRabadiya 11-Aug-14 8:14am    
hmm thank you sir i will try it soon :))
UttamRabadiya 12-Aug-14 6:13am    
i try but its failed :((
Herman<T>.Instance 13-Aug-14 8:33am    
what was the exception? something about threading?
Herman<T>.Instance 13-Aug-14 10:20am    
if you want to show in a textbox, you have to Invoke! the textBox.
Read about that

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


and another change on ProgressChanged.

C#
var filename = Convert.ToString(e.UserState);
label1.Text = "Downloading Update File " + filename;
 
Share this answer
 
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.
 
Share this answer
 

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