ForgottenOne
ForgottenOne

Reputation: 85

c# detect an open web browser

I am writing a program that searches certain web pages before closing. I would like my program to open a NEW WINDOW using the DEFAULT BROWSER. I can have my program focus the newest window instance, and then it will close that instance once it is done.

I have been staring at WebBrowser.Navigate and System.Diagnostics.Process.Start(target) all day but I cant find the sweet spot with either of them.

WebBrowser.Navigate always opens IE, I have been looking in the API and can't find a way to change the program used. Does anyone else see something that I dont? Is there a way to change the application used?

System.Diagnostics.Process.Start(target) opens in a new tab, not a new window like navigate does. However none of the overloaded functions from the API have a way of saying "create a new instance or window".

this is my issue, they both have pieces that I want, but I cant figure out how to get the pieces I need for either one.

I would be extremely grateful for you help. I have been looking for hours now and I can seem to come to a solution.

code sample for Jester:

            Process defaultbrowser = new Process();


            defaultbrowser.StartInfo.CreateNoWindow = true;
            defaultbrowser = Process.Start(target);       

            int waitTime = Convert.ToInt32(numericUpDown2.Value);
           System.Threading.Thread.Sleep(waitTime*1000);

           defaultbrowser.CloseMainWindow();
           defaultbrowser.Close();

furthermore my Close() function is causing a runtime error that says;

System.NullReferenceException: Object reference not set to an instance of an object.

which seems silly because too me the above code makes me think that my defaultbrowser is an instance of a process, which is then supposed to be able to call the non-static function "close()".

Upvotes: 1

Views: 3449

Answers (3)

Jester
Jester

Reputation: 3317

ok If I got your problem right you are looking for a way to open a web page in the "default" browser.

That can be done by simply make a new process like:

Process.Start("http://google.com");

If you would like to control witch browser gets used you can do it by passing the web address to the browser's exe file as a parameter:

System.Diagnostics.Process.Start("PATH to exe", "Command Line Arguments");

To start the process in the new window pass a ProcessInfo object to the Process.Start And set the CreateNoWindow more info on that

Upvotes: 2

Panayotis
Panayotis

Reputation: 1783

You should use System.Diagnostics.Process like that:

Process Chrome = new Process(); //Create the process
Chrome.StartInfo.FileName = @"C:\Program Files\Google\Chrome\Application\chrome.exe";  // Needs to be full path
Chrome.StartInfo.Arguments = ""; // If you have any arguments
Chrome.Start();

Upvotes: 0

Neeraj
Neeraj

Reputation: 4489

Hey To check if it's loaded wherever, do:

if(browser.ReadyState == WebBrowserReadyState.Complete) {
// It's Open!
}

Upvotes: 0

Related Questions