我有一个C#Web应用程序,我想在其中一个接一个地执行Unix命令。我希望出现命令提示符窗口,然后在每个命令执行之后,移至下一个命令,然后退出该窗口。我已经尝试了以下方法,但是命令提示符窗口没有出现,并且每次等待后命令都没有执行
public void connecterOne()
{
try
{
using (Process p = new Process())
{
// set start info
p.StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WorkingDirectory = @"c:\"
};
// event handlers for output & error
p.OutputDataReceived += p_OutputDataReceived;
p.ErrorDataReceived += p_ErrorDataReceived;
// start process
p.Start();
// send command to its input
p.StandardInput.Write("ftp server" + p.StandardInput.NewLine);
p.StandardInput.Write("username" + p.StandardInput.NewLine);
p.StandardInput.Write("pwd" + p.StandardInput.NewLine);
string output = p.StandardOutput.ReadToEnd();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Process p = sender as Process;
if (p == null)
return;
Console.WriteLine(e.Data);
}
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Process p = sender as Process;
if (p == null)
return;
Console.WriteLine(e.Data);
}
如何一一执行命令并打开命令提示符窗口,其中显示了正在执行的命令。我没有收到任何错误,但是没有看到输出。