Get the process id of a program launched using C # Process.Start

Thanks in advance for your help!

I am currently developing a program in C # 2010 that runs PLink (Putty) to create an SSH tunnel. I am trying to make the program able to track every tunnel that is open, so the user can terminate those instances that are no longer needed. I am currently using System.Diagnostics.Process.Start to run PLink (currently using Putty). I need to determine the PID of each plink program when it starts, so that the user can finish it as they wish.

The question is how to do this, and am I using the correct .Net namespace or is 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);
}
+3
3

:

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;
}
+4

Process.Start Process. Process.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.
    }
+2

I'm not sure if I understand correctly, but Process.Start(at least the overload you are using) will return Processand then Processhave a property Id.

0
source

Source: https://habr.com/ru/post/1749967/


All Articles