下载的exe文件有0个字节

时间:2015-08-25 16:38:56

标签: .net vb.net

我正在努力学习VB.NET。我想在桌面上下载file.exe并执行它。我写了这段代码:

Imports System.IO
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim mani As String = My.Computer.FileSystem.SpecialDirectories.Desktop 
        My.Computer.Network.DownloadFile("www.example.com/file.exe", mani & "\mani.exe")
        Process.Start(mani & "\mani.exe")
    End Sub
End Class

当我启动应用程序时,它会将文件下载到名为mani.exe的桌面。原始文件为400kb,下载的文件为0kb,文件已损坏。

当我使用浏览器下载文件时,它运行正常并且大小合适。

这是debug的输出:

The thread 0x754 has exited with code 259 (0x103).
The thread 0x748 has exited with code 259 (0x103).
The thread 0x15b8 has exited with code 0 (0x0).
The thread 0x128c has exited with code 259 (0x103).
'Download and Execute.vshost.exe' (CLR v4.0.30319: Download and Execute.vshost.exe): Loaded 'C:\Users\T0X\Documents\Visual Studio 2013\Projects\Download and Execute\Download and Execute\bin\Debug\Download and Execute.exe'. Symbols loaded.
Step into: Stepping over property
'Download_and_Execute.My.MyProject.get_Application'.  To step into properties, go to Tools->Options->Debugging and uncheck 'Step over properties and operators (Managed only)'.
'Download and Execute.vshost.exe' (CLR v4.0.30319: Download and Execute.vshost.exe): Loaded 
'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Remoting\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll'. Cannot find or open the PDB file.
'Download and Execute.vshost.exe' (CLR v4.0.30319: Download and Execute.vshost.exe): Loaded 
'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
The program '[7124] Download and Execute.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
The program '[7124] Download and Execute.vshost.exe: Program Trace' has exited with code 0 (0x0).

1 个答案:

答案 0 :(得分:0)

您是否尝试使用WebClient.DownloadFile功能?

有关于Network.DownloadFile的说明:

  

DownloadFile方法不会发送可选的HTTP标头。   如果可选,某些服务器可能会返回500(内部服务器错误)   用户代理标头丢失。要发送可选标头,您必须   使用WebClient类构造请求。