简体   繁体   中英

How do I code an automated download in VB?

Right now I'm trying to code a tool that shall be capable of downloading a file from a specific website, beside some other stuff.

My current solution is, that I open the website from where the file shall be downloaded in Internet Explorer, wait some seconds so the website can be loaded and then send a keystroke to Internet Explorer, which is ALT+S, to save the file to the standard download location.

However, my code fails in the line where the keystroke should be sent. You'll find it below of course plus the error it throws.


Some facts I believe you'll find useful to know:

  • My application shall be a console app, no forms app
  • I'm using the Internet Explorer for this so that I can be sure the application works for every Windows PC
  • I expect the Internet Explorer to have the default configuration for downloading files to be set to "User prompt", ie it's asking wether the file shall be openend or saved
  • The website, as you'll see in the code below, is a third-party website, so I don't have direct access to the files being hosted on the server

What I specifically like to know is:

  • Can I solve this problem somehow smarter? Are there other, perhaps easier ways, to download a file than what I did?
  • How can I make my application wait for the website to be loaded or even wait for the "Open - Save - Cancel" prompt to show up?
  • How do I get the "SendKeys" part to work correctly?

And most important: Please note that I'm a hobby programmer and just started with VB a few weeks ago. If I missed out some important information or my code looks weird, well, then that's why :)


Sub Download()

    Dim IE As Object
    IE = CreateObject("InternetExplorer.Application")

    Console.WriteLine("Start Download")
    IE.Navigate("https://toolslib.net/downloads/finish/1-adwcleaner/") 'Opens the website in Internet Explorer
    Do 'Waits for Internet Explorer to be launched before going on
        If CBool(Process.GetProcesses.Where(Function(P As Process) _ 
                 P.ProcessName = "iexplore").Count) Then
            Exit Do
        End If
        Threading.Thread.Sleep(100)
    Loop
    IE.Visible = True

    Threading.Thread.Sleep(5000) 'Some time for the website to load
    IE.SendKeys("%{S}", True)

End Sub

When running this code it throws the following error.

Note: Line 67 is the line where IE.SendKeys("%{S}", True) stands.

Unhandled Exception: System.ArgumentException: The process {0} wasn't found.
   at Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   bei Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   at MyApp.Module1.Download() in C:\Users\User\Documents\Visual Studio 2017\Projects\MyApp\MyApp\Module1.vb:Line 67.

I used WebBrowser control in form application and its worked fine me!

                SendKeys.Send("+{TAB}")
                System.Threading.Thread.Sleep(1000)
                SendKeys.Send("{ENTER}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM