我有一个simpel程序,在给定的端口和主机上运行漏洞扫描。现在我必须找到一种方法来关闭从我的c#表单运行的批处理文件。 我必须能够从一个按钮关闭批处理文件,即使它尚未完成。我不知道,也没找到办法。
编辑:添加了更多代码,但仍然给出了错误"当前环境中并不存在过程"
private void button10_Click(object sender, EventArgs e)
{
if (button10.Text == "Scan")
{
int port = (int)numericUpDown2.Value;
string path = Directory.GetCurrentDirectory();
string strCommand = path + "/SystemFiles/nikto/nikto.bat";
string host = textBox5.Text;
Console.WriteLine(strCommand);
richTextBox5.Text += "Starting Nikto Vulnerability Scan On " + host + " On Port " + port + System.Environment.NewLine;
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = strCommand;
startInfo.Arguments = "-h " + host + " -port " + port + textBox6.Text;
process.StartInfo = startInfo;
process.Start();
richTextBox5.Text += "Vulnerability Scan Started On " + host + " On Port " + port + System.Environment.NewLine;
button10.Text = "Cancel";
}
else
{
process.Kill();
button10.Text = "Scan";
}
}
答案 0 :(得分:1)
您可以在流程对象
中使用kill方法process.Kill()
答案 1 :(得分:0)
以下是完整的答案。
public partial class Form1 : Form
{
private System.Diagnostics.Process _process;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (_process == null || _process.HasExited)
{
_process = new Process();
}
else
{
_process.Kill();
_process = null;
button10.Text = "Scan";
return;
}
int port = (int)numericUpDown2.Value;
string path = Directory.GetCurrentDirectory();
string strCommand = path + "/SystemFiles/nikto/nikto.bat";
string host = textBox5.Text;
Console.WriteLine(strCommand);
richTextBox5.Text += "Starting Nikto Vulnerability Scan On " + host + " On Port " + port + System.Environment.NewLine;
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = strCommand,
Arguments = "-h " + host + " -port " + port + textBox6.Text
};
_process.StartInfo = startInfo;
_process.Start();
richTextBox5.Text += "Vulnerability Scan Started On " + host + " On Port " + port + System.Environment.NewLine;
button10.Text = "Cancel";
}
}