views:

370

answers:

3

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);
}
+1  A: 

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.
    }
Femaref
thank you, exactly what i was looking for!
ThaKidd
+1  A: 

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;
}
Tim S. Van Haren
exactly what i was looking for. thank you!
ThaKidd
A: 

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.

ho1