I have a task killer program that has a listbox of current running processes. After an item is selected, a button click runs code to kill the selected task. That all works fine, but what I need help with is manipulating the string output on my form that displays what the last task killed was. For instance, if I open up calculator and kill that task, my string output is: "System.Diagnostics.Process (calc)". I would prefer if this was to just say "calc". So I need to be able to strip the "System.Diagnostics.Process ( )"
public bool KillProcess(ListBox PList, TextBox Killed)
{
bool Kill = true;
string x = "";
Killed.Text = "";
if (PList.SelectedItem != null)
{
foreach (Process p in Process.GetProcesses(Environment.MachineName))
{
x = PList.SelectedItem.ToString();
if (p.ProcessName.Equals(x))
{
try
{
p.Kill();
//*************** Here is where I have the process killed placed into a textbox
//Which I would like to not say "System.Diagnostics.Process..."
Killed.Text = p.ToString();
}
catch
{
MessageBox.Show(p.ProcessName.ToString() + " cannot be killed. ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Kill = false;
}
}
}
}
else
{
MessageBox.Show("Please select a process to be killed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Kill = false;
}
return Kill;
}