1

I have a C# application which functions as an updater. The program checks a file on the internet for a current version, and if that version is greater than the installed version, it will proceed to download an executable (not .zip) file from a web site using a url and a webclient. Relevant Code (Minus Personal Information/Web addresses):

Main Thread:

string webUrl = @"http://mywebsite/executable" + NewVersion + ".exe";

                    Thread thread = new Thread(() => {
                        try
                        {
                            WebClient updateDownloader = new WebClient();
                            Uri newExecutable = new Uri(webUrl);
                            updateDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                            updateDownloader.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                            updateDownloader.DownloadFileAsync(newExecutable, @"C:\Users\Public\MyExecutable.exe");
                        }
                        catch(Exception WebError) { MessageBox.Show(WebError.ToString()); }
                    });
                    button1.Enabled = false;
                    thread.Start();

Download Progress Changed Event:

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    this.BeginInvoke((MethodInvoker)delegate {
        double bytesIn = double.Parse(e.BytesReceived.ToString());
        double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
        double percentage = bytesIn / totalBytes * 100;
        label1.Text = "Downloaded " + (e.BytesReceived * .001) + "KB of " + (e.TotalBytesToReceive * .001)+"KB";
        progressBar1.Style = ProgressBarStyle.Blocks;
        progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
        if(progressBar1.Value == 100)
        {
            label1.Text = "100%";
        }
    });
}

Download Completed Event:

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    this.BeginInvoke((MethodInvoker)delegate {
        label2.Text = "Completed";
        button1.Enabled = true;
        MessageBox.Show("Download Complete! Press OK to launch the new version.", "Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
        try
        {
            Process.Start(@"C:\Users\Public\MyExecutable.exe");
        }
        catch(Exception ee) { MessageBox.Show("Error: " + ee.ToString()); }
        Application.Exit();
    });
}

My Issue Is: The application will download almost the entire file, but will stop near the end (see image). This leaves the executable unable to be run (also see image).Almost Downloads Entire File Executable is left unable to be runCMD Filesize Output

I have tried downloading the same file with Chrome and Firefox, and 90% of the time it will work just fine. The other 10% of the time it will recognize the file to download, but will never progress from 0 bytes/0 bytes.

Running the same application on different PCs works fine. The firewall is not configured to block this application or any downloads and it doesn't work with Windows Smart Screen on or off. (Same goes for Admin privileges and compatibility mode)

The weirdest part is on this same machine just a week ago with the same program it worked just fine.

TL;DR: My program won't download a file without it being corrupted, while from a web browser it [usually] works just fine. This same program works fine on every other PC I've tried.

Any assistance would be appreciated, including code change suggestions, and possible solutions to the issue at hand.

Update I let the program run until it had supposedly finished downloading the file. Here is the specific error:

************** Exception Text **************
System.ComponentModel.Win32Exception (0x80004005): The specified executable is not a valid application for this OS platform.
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)
   at MyExecutable.Form1.<client_DownloadFileCompleted>b__7_0()
   at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1648.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
MyExecutable
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/admin/Desktop/MyExecutable.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1586.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1647.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1586.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1586.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1647.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1586.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Question Asker
  • 199
  • 2
  • 18
  • I'm not sure about your issue, but `DownloadFileAsync` returns a Task, so you don't need to run that in its own Thread. – ProgrammingLlama May 31 '17 at 03:01
  • @john Thanks for that tip. I haven't worked much with threading and got that idea from another SO answer :) – Question Asker May 31 '17 at 03:16
  • Are you sure `MyExecutable.exe` isn't in use by another process (e.g. if it is already running in another window)? Any chance it is being quarantined by an anti-virus program? – John Wu May 31 '17 at 03:20
  • @JohnWu Yes, I have verified that all instances of MyExecutable.exe have been closed. I have even tried deleting the local copy of MyExecutable.exe before running the Updating application. – Question Asker May 31 '17 at 03:22
  • Have you checked the file size and/or compared its contents in a binary file viewer? Is it identical to a known good file? Or is it truncated or missing portions? – John Wu May 31 '17 at 03:23
  • @JohnWu The file size of the downloaded file is shown in the picture. The first number is what it downloads as and the second is what it should be. I have not compared it in a binary file viewer, but the file size is not correct. – Question Asker May 31 '17 at 03:24
  • Do you have privileges that would allow you to set up a remote debugger? [link](https://msdn.microsoft.com/en-us/library/y7f5zaaa.aspx) – Poosh May 31 '17 at 03:27
  • @link I can contact the machine owner to find out. – Question Asker May 31 '17 at 03:32
  • @link The application doesn't appear to be throwing any errors though. – Question Asker May 31 '17 at 03:33
  • With respect, the picture does not show the file size. It shows your program's output. Have you looked at the file in Explorer and checked the size? – John Wu May 31 '17 at 03:41
  • @JohnWu You're right! My bad :) I have added the output of the `dir` command in a command prompt window to the original question. I have renamed the files for convenience. – Question Asker May 31 '17 at 03:47
  • Thanks. One possibility is that one of your event handlers is throwing an uncaught exception. Maybe comment out all of your event handlers (other than `Completed`) temporarily to see if that resolves the issue? Also, I'm not 100% sure that it's a great idea to call `Application.Exit()` from within `BeginInvoke`, I would comment that out too for now. – John Wu May 31 '17 at 03:52
  • @JohnWu I did as you suggested and it seems to render the same result. The download file had a file size of 1,365,138 bytes, which is 878 bytes short of the complete file. _Perhaps it is interesting to note that if you leave the program open, it will slowly add a few bytes to the file until it says it has completed the download. Even then, though, the file will not run._ – Question Asker May 31 '17 at 04:00
  • Does this machine use a proxy server to access the internet? Have you [configured your application](https://msdn.microsoft.com/en-us/library/kd3cf2ex(v=vs.110).aspx) to use it? – John Wu May 31 '17 at 17:57
  • @JohnWu It doesn't use a proxy server, and a web browser that isn't configured to use one is able to access the file. – Question Asker May 31 '17 at 18:19
  • Any antivirus? Running out of straws here. – John Wu May 31 '17 at 19:06
  • As a means of narrowing the problem, have you tried downloading a different file, or the same file but from a different/known-good location? – John Wu May 31 '17 at 19:11
  • @JohnWu I have tried downloading a different executable from the root directory on the server and it doesn't work either. Only active antivirus is Windows Defender and I set it to ignore the directory in question. – Question Asker May 31 '17 at 20:20
  • Maybe after running the application, you could check the Event Viewer for logs over the timespan the application executed to identify if some other process or network connectivity issues are interfering. – Poosh Jun 01 '17 at 05:11
  • I don't know if you solved this, but does an executable with the **version included in the name exist?** `"http://mywebsite/myexecutable" + NewVersion + ".exe"` - Does an executable with `NewVersion` in it's name exist? – Momoro Feb 16 '20 at 03:29

0 Answers0