加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

获取在C#中创建的进程的pid

发布时间:2020-12-15 06:27:26 所属栏目:百科 来源:网络整理
导读:让我们说我正在尝试使用以下代码创建一个新进程: System.Diagnostics.Process p = new System.Diagnostics.Process();p.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeB
让我们说我正在尝试使用以下代码创建一个新进程:
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:

MessageBox.Show(p.Id);

这一行给了我“没有进程与此对象相关联”.错误.知道为什么会出现这个错误吗?

解决方法

做这个 System.Diagnostics.Process.GetProcessesByName( “processname”)[0] .ID.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读