我是新手编写代码并尝试在C#中编写一个程序,它将基本命令发送到Cmd提示符我正在使用带有此程序的Windows窗体。以下是我正在使用的代码。我可以获取要发送的命令,但cmd窗口关闭,然后才能看到其中一些输出。
private void button1_Click(object sender, EventArgs e)
{
//ping command
string pcname;
string strCmdText;
pcname = Convert.ToString(PC.Text);
strCmdText = $"/c ping {pcname}";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
//ping pcname
}
private void button2_Click(object sender, EventArgs e)
{
//trace route command
string pcname;
string strCmdText;
pcname = Convert.ToString(PC.Text);
strCmdText = $"/c tracert {pcname} ";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
//tracert pcname
}
private void button3_Click(object sender, EventArgs e)
{
//last reboot command
string pcname;
string strCmdText;
pcname = Convert.ToString(PC.Text);
//wmic /node:DLAT97025 os get lastbootuptime
strCmdText = $"/c wmic /node:{pcname} os get lastbootuptime";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
private void button4_Click(object sender, EventArgs e)
{
//shutdown command
string pcname;
string shutdown;
string strCmdText;
pcname = Convert.ToString(PC.Text);
shutdown = Convert.ToString(ReasonForShutdown.Text);
strCmdText = $"/c shutdown /m \\{pcname} /c {shutdown}";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
//shutdown /m \\pcname /c shutdown
}
private void button5_Click(object sender, EventArgs e)
{
//restart command
string pcname;
string restart;
string strCmdText;
pcname = Convert.ToString(PC.Text);
restart = Convert.ToString(ReasonforRestart.Text);
strCmdText = $"/c shutdown /r \\{pcname} /c {restart}";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
//shutdown /r \\pcname /c restart
}