开发者

What’s the difference between Process and ProcessStartInfo in C#?

What’s the difference between Process and ProcessStartInfo? I’ve used both to launch external pro开发者_运维技巧grams but there has to be a reason there are two ways to do it. Here are two examples.

Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.StartInfo.Arguments = "ProcessStart.cs";
notePad.Start();

and

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "notepad.exe";
startInfo.Arguments = "ProcessStart.cs";
Process.Start(startInfo);


They are pretty close to the same, both are from the Process class. And there are actually 4 other overloads to Process.Start other than what you mentioned, all static.

One is a static method way to do it. It returns the Process object representing the process that is started. You could for example start a process with a single line of code by using this way.

And the other is a member method way to do it which reuses the current object instead of returning a new one.


Heh.

If you look closely at your code, you will note that they are both using the same classes. The StartInfo property in your first example is, unsurprsingly, a ProcessStartInfo. In your second example, you call the static .Start method on the Process class.

So what are the differences? Significant. They're different classes. One is for launching processes, one is for saying which process to launch (and lots of other little things, like capturing output, etc). In the first case, you just use the default ProcessStartInfo property that the class has.


It seems that ProcessStartInfo is a subset of Process if you look at the members of notePad variable below

Process notePad = new Process(); You will notice that StartInfo has type (or of class) ProcessStartInfo which is why the two initializations are the same notePad.StartInfo.FileName = "notepad.exe"; Vs startInfo.FileName = "notepad.exe"; Since Process is the full class I would think it can do everything ProcessStartInfo can do plus extra But Don't take my word for it I only less than a year experience in .Net

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜