Invalid Operation Exception from C# Process Class

George2 picture George2 · Jul 20, 2009 · Viewed 46.9k times · Source

When I use VSTS debugger to see the properties of instance of class Process, many of the properties are marked with InvalidOperationException. Why? Am I doing anything wrong?

I am using VSTS 2008 + C# + .Net 2.0 to develop a console application.

Here is my code:

System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "IExplore.exe";
myProcess.StartInfo.Arguments = @"www.google.com";
myProcess.StartInfo.Verb = "runas";
myProcess.Start();

And a screenshot of the debugger:

enter image description here

Answer

Jon Skeet picture Jon Skeet · Jul 20, 2009

Had you actually started the process when the debugger picture was taken? That's the screenshot I'd expect to see before the Start() method is called.

Note that the common pattern is to create a ProcessStartInfo, populate it, and then call the static Process.Start(startInfo) method. That makes it conceptually simpler: you don't see the Process object until it's been started.