得到一个进程的PID在C#创建 [英] Getting a pid of a process created in C#

查看:142
本文介绍了得到一个进程的PID在C#创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我想创建一个包含以下代码的新工艺:

Lets say that I'm trying to create a new process with the following code:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
p.StartInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\AwesomeFile.exe";
p.StartInfo.Arguments = "parameter1 parameter2";
p.StartInfo.CreateNoWindow = true;
p.Start();

和正确的下一行,我会试图获取过程的PID具有以下行:

and right in the next line, I'll try to get a pid of that process with the following line:

MessageBox.Show(p.Id);

这行是给我的无过程是与此对象关联。错误。任何想法,为什么会出现这个问题?

This line is giving me the "No process is associated with this object." error. Any idea as to why this error occurs?

推荐答案

做到这一点
System.Diagnostics.Process.GetProcessesByName(processname )[0] .ID。

Do this System.Diagnostics.Process.GetProcessesByName("processname")[0].Id.

这篇关于得到一个进程的PID在C#创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆