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