Link to home
Start Free TrialLog in
Avatar of T Hoecherl
T HoecherlFlag for United States of America

asked on

Close Browser window with VB.net

I have an application that includes a timer.  Every hour I have code which downloads a file from a website and saves it to a specific location.  The application is working properly.  However, before I download the file I need to access a website and the process of accessing it is what generates the file that I then download.

My code to access the website is Process.Start("url address").  This works, as well.  But what I do not know how to do is close the website after the file has been generated so at the end of the day I have several browser windows open to the same url.  I found some references to using javascript, but I don't know javascript so I am at a loss.  How can I close the browser window from my VB.net program?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

You can kill it afterwards

Process.Start returns a process object and you can call the Kill method on it

http://visualbasic.about.com/od/usingvbnet/a/prstrt.htm
Avatar of T Hoecherl

ASKER

OK.  I have this:

Dim myProcess As Process = Process.Start("url address")
        myProcess.Kill()

The browser opens to the correct url, but the myProcess.Kill() command generates this error:

NullReferenceException was unhandled
Object reference not set to an instance of an object.

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks CodeCruiser.  I had to use this code to finally get it to work:

        Dim theProcesses() As Process = System.Diagnostics.Process.GetProcessesByName("Iexplore")
        For Each currentProcess As Process In theProcesses
             If currentProcess.MainWindowTitle.Equals("text in url") Then
                currentProcess.Kill()
            End If
        Next

but it does now do the job.  Thanks again.