Valentin Radu
Valentin Radu

Reputation: 649

WaitForExit default browser

I'm using this code:

Process p = new Process();
ProcessStartInfo si = new ProcessStartInfo();
si.UseShellExecute = true;
si.FileName = Url;
p = Process.Start(si);
p.WaitForExit();

To open the "Url" in the default browser. I want to wait for the user to close that browser and then execute the code further. But after opening the Url in IE (my default browser), it throws an Object reference not set to an instance of object and highlights

p.WaitForExit();

What is the problem? I just want to wait until it finishes. Thanks for any advice.

Upvotes: 1

Views: 1805

Answers (2)

HatSoft
HatSoft

Reputation: 11201

The ProcessStartInfo.FileName gets or sets the application or document to start. What I have done is passed the application path in the FileName so when Process.Start creates it looks for Arguments and creates an instance in your case it does not

Process p = new Process();
ProcessStartInfo si = new ProcessStartInfo();
p.StartInfo = si;
si.UseShellExecute = true;
si.FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe";
si.Arguments = "http://www.stackoverflow.com";
p.Start();
p.WaitForExit();

Upvotes: 2

STO
STO

Reputation: 10658

Process.Start(new ProcessStartInfo()
            {
                UseShellExecute = true,
                FileName = "http://google.com",                
            }).WaitForExit();

Upvotes: 0

Related Questions