我有一个任务杀手程序,它具有当前正在运行的进程的列表框。选择一个项目后,点击按钮将运行代码以终止所选任务。这一切工作正常,但我需要帮助的是操纵我的窗体上的字符串输出,显示最后一项任务是什么。例如,如果我打开计算器并终止该任务,那么我的字符串输出为:“System.Diagnostics.Process(calc)”。我宁愿如果这只是说“calc”。所以,我需要能够剥离而不是在c中操作输出字符串#
Killed.Text = p.ToString();
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;
}
你为什么不简单地使用p.ProcessName? – BlackBear 2012-03-23 14:42:57