C#WebClient未完成文件下载

时间:2017-05-31 02:56:39

标签: c# webclient webclient-download

我有一个C#应用程序,它可以作为更新程序。程序检查互联网上的文件是否有当前版本,如果该版本大于安装版本,它将继续使用URL和webclient从网站下载可执行文件(而不是.zip)。相关守则(减号个人信息/网址):

主线程

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

下载进度更改事件:

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%";
        }
    });
}

下载已完成的活动:

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

我的问题是: 应用程序将下载几乎整个文件,但将在结束时停止(见图)。这使得可执行文件无法运行(也见图像)。Almost Downloads Entire File  Executable is left unable to be run CMD Filesize Output

我尝试使用Chrome和Firefox下载相同的文件,90%的时间它可以正常工作。另外10%的时间它会识别要下载的文件,但永远不会从0 bytes/0 bytes进展。

在不同的PC上运行相同的应用程序工作正常。防火墙未配置为阻止此应用程序或任何下载,并且它无法启用或禁用Windows智能屏幕。 (管理员权限和兼容性模式也是如此)

最奇怪的部分是在一周前的同一台机器上使用相同的程序工作得很好。

TL; DR : 我的程序不会在没有损坏的情况下下载文件,而从Web浏览器[通常]工作正常。这个程序在我试过的其他所有PC上运行正常。

我们将不胜感激,包括代码更改建议以及手头问题的可能解决方案。

更新 我让程序运行,直到它应该完成下载文件。以下是具体错误:

************** 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
----------------------------------------

0 个答案:

没有答案