Click here to Skip to main content
12,763,337 members (34,185 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# VB VB.NET
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();

         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);



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 9-Aug-14 3:38am
Updated 13-Aug-14 5:18am
digimanus 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 :((
digimanus 13-Aug-14 8:33am
what was the exception? something about threading?
UttamRabadiya 13-Aug-14 8:37am
i have already string file that have the downloading file name and when i try to put the string file on will say the name 'file' does not exists in current context
UttamRabadiya 13-Aug-14 8:39am
i was thinking if i even can print the value from XML file like if version = 1.2 than it will read file name for version 1.2. i just have this idea but i am still thinking how i am gonna apply it :)
digimanus 13-Aug-14 10:20am
if you want to show in a textbox, you have to Invoke! the textBox.
Read about that
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Finally Solve it :))
i just adjusted some codes like here
in while Progress in DoWork.

and another change on ProgressChanged.

 var filename = Convert.ToString(e.UserState);
 label1.Text = "Downloading Update File " + filename;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 17 Aug 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100