获取使用 C# Process.Start 启动的程序的进程 ID

发布于 2024-09-05 18:57:56 字数 653 浏览 7 评论 0原文

预先感谢您的所有帮助!

我目前正在 C# 2010 中开发一个程序,该程序启动 PLink (Putty) 来创建 SSH 隧道。我试图使程序能够跟踪每个打开的隧道,以便用户可以终止那些不再需要的实例。我目前正在使用 System.Diagnostics.Process.Start 来运行 PLink(当前正在使用 Putty)。我需要确定每个 plink 程序启动时的 PID,以便用户可以随意终止它。

问题是如何做到这一点,我是否使用了正确的 .Net 名称空间,或者是否有更好的东西?

代码片段:

private void btnSSHTest_Click(object sender, EventArgs e)
{
    String puttyConString;
    puttyConString = "-ssh -P " + cboSSHVersion.SelectedText + " -" + txtSSHPort.Text + " -pw " + txtSSHPassword.Text + " " + txtSSHUsername.Text + "@" + txtSSHHostname.Text;
    Process.Start("C:\\Program Files (x86)\\Putty\\putty.exe", puttyConString);
}

Thanks in advance for all of your help!

I am currently developing a program in C# 2010 that launches PLink (Putty) to create a SSH Tunnel. I am trying to make the program able to keep track of each tunnel that is open so a user may terminate those instances that are no longer needed. I am currently using System.Diagnostics.Process.Start to run PLink (currently Putty being used). I need to determine the PID of each plink program when it is launched so a user can terminate it at will.

Question is how to do that and am I using the right .Net name space or is there something better?

Code snippet:

private void btnSSHTest_Click(object sender, EventArgs e)
{
    String puttyConString;
    puttyConString = "-ssh -P " + cboSSHVersion.SelectedText + " -" + txtSSHPort.Text + " -pw " + txtSSHPassword.Text + " " + txtSSHUsername.Text + "@" + txtSSHHostname.Text;
    Process.Start("C:\\Program Files (x86)\\Putty\\putty.exe", puttyConString);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捶死心动 2024-09-12 18:58:22

我不确定我是否理解正确,但是 Process.Start (至少是您正在使用的重载)将返回一个 Process,然后返回 Process< /code> 有一个 Id 属性。

I'm not sure if I understand correctly, but Process.Start (at least the overload you're using) will return a Process and then that Process has an Id property.

↘紸啶 2024-09-12 18:58:14

Process.Start 返回一个 Process 对象。使用 Process.Id 属性查找 ID。

private void btnSSHTest_Click(object sender, EventArgs e)
    {
        String puttyConString;
        puttyConString = "-ssh -P " + cboSSHVersion.SelectedText + " -" + txtSSHPort.Text + " -pw " +        txtSSHPassword.Text + " " + txtSSHUsername.Text + "@" + txtSSHHostname.Text;
        Process started = Process.Start("C:\\Program Files (x86)\\Putty\\putty.exe", puttyConString);
        //do anything with started.Id.
    }

Process.Start returns a Process object. Use the Process.Id property to find out the id.

private void btnSSHTest_Click(object sender, EventArgs e)
    {
        String puttyConString;
        puttyConString = "-ssh -P " + cboSSHVersion.SelectedText + " -" + txtSSHPort.Text + " -pw " +        txtSSHPassword.Text + " " + txtSSHUsername.Text + "@" + txtSSHHostname.Text;
        Process started = Process.Start("C:\\Program Files (x86)\\Putty\\putty.exe", puttyConString);
        //do anything with started.Id.
    }
一口甜 2024-09-12 18:58:06

你可以这样做:

private void btnSSHTest_Click(object sender, EventArgs e)
{
    String puttyConString;
    puttyConString = "-ssh -P " + cboSSHVersion.SelectedText + " -" + txtSSHPort.Text + " -pw " + txtSSHPassword.Text + " " + txtSSHUsername.Text + "@" + txtSSHHostname.Text;
    Process putty = Process.Start("C:\\Program Files (x86)\\Putty\\putty.exe", puttyConString);
    int processId = putty.Id;
}

You can do this:

private void btnSSHTest_Click(object sender, EventArgs e)
{
    String puttyConString;
    puttyConString = "-ssh -P " + cboSSHVersion.SelectedText + " -" + txtSSHPort.Text + " -pw " + txtSSHPassword.Text + " " + txtSSHUsername.Text + "@" + txtSSHHostname.Text;
    Process putty = Process.Start("C:\\Program Files (x86)\\Putty\\putty.exe", puttyConString);
    int processId = putty.Id;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文