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.

有帮助吗?

解决方案

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();

其他提示

Process.Start(new ProcessStartInfo()
            {
                UseShellExecute = true,
                FileName = "http://google.com",                
            }).WaitForExit();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top