C# 中 Process 和 ProcessStartInfo 有什么区别?
Process
和 ProcessStartInfo
之间有什么区别?我已经使用这两种方法来启动外部程序,但是有两种方法可以做到这一点一定是有原因的。这里有两个例子。
Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.StartInfo.Arguments = "ProcessStart.cs";
notePad.Start();
和
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "notepad.exe";
startInfo.Arguments = "ProcessStart.cs";
Process.Start(startInfo);
What’s the difference between Process
and ProcessStartInfo
? I’ve used both to launch external programs 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们非常接近,都来自 Process 类。实际上还有 Process.Start 的其他 4 个重载 除了你提到的以外,都是静态的。
一种是静态方法方法。它返回表示已启动进程的 Process 对象。例如,您可以使用这种方式用一行代码启动一个进程。
另一种是成员方法重用当前的方法对象而不是返回一个新对象。
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.
呵呵。
如果仔细查看代码,您会发现它们都使用相同的类。不出所料,第一个示例中的
StartInfo
属性是ProcessStartInfo
。在第二个示例中,您在Process
类上调用静态.Start
方法。那么有什么区别呢?重要的。他们是不同的班级。一个用于启动进程,一个用于说明要启动哪个进程(以及许多其他小事情,例如捕获输出等)。在第一种情况下,您只需使用该类具有的默认
ProcessStartInfo
属性。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, aProcessStartInfo
. In your second example, you call the static.Start
method on theProcess
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.那么
ProcessStartInfo
似乎是Process
的子集如果您查看下面的
notePad
变量的成员Process notePad = new Process() , ;
您会注意到 StartInfo 具有类型(或类)
ProcessStartInfo
这就是为什么两个初始化是相同的
notePad.StartInfo.FileName = "notepad.exe";
与startInfo.FileName = "notepad.exe";
由于
Process
是完整的类,我认为它可以完成ProcessStartInfo
可以做的所有事情,再加上额外的功能,但不要相信我的话,我在 .网It seems that
ProcessStartInfo
is a subset ofProcess
if you look at the members ofnotePad
variable belowProcess 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";
VsstartInfo.FileName = "notepad.exe";
Since
Process
is the full class I would think it can do everythingProcessStartInfo
can do plus extra But Don't take my word for it I only less than a year experience in .Net