Get Process ID of Program Started with C# Process.Start
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", puttyCon开发者_运维问答String);
}
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;
}
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.
}
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.
精彩评论