Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将命令/参数发送到命令提示符';它已经打开了_C#_Windows_Command Line Arguments_Command Prompt - Fatal编程技术网

C# 将命令/参数发送到命令提示符';它已经打开了

C# 将命令/参数发送到命令提示符';它已经打开了,c#,windows,command-line-arguments,command-prompt,C#,Windows,Command Line Arguments,Command Prompt,所以我知道我已经可以启动一个cmd.exe进程,并在它打开时将ping google.com输入其中。这很容易做到,如下所示: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; usin

所以我知道我已经可以启动一个
cmd.exe
进程,并在它打开时将
ping google.com
输入其中。这很容易做到,如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SendComandToCMD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.UseShellExecute = false;

            Process process = Process.Start(processStartInfo);

            if (process != null)
            {            
                process.StandardInput.WriteLine("ping google.com");

                process.StandardInput.Close(); // line added to stop process from hanging on ReadToEnd()

                lblResult.Text = process.StandardOutput.ReadToEnd(); // return CMD.exe output to the Form label here
            }
        }
    }
}
和繁荣,如此容易

但是,下面的示例理论上符合我的要求,但它崩溃了:

namespace SendComandToCMD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process process = Process.GetProcessById(22180); // Grab a process which is already running by its ID

            if (process != null)
            {            
                process.StandardInput.WriteLine("ping google.com");

                process.StandardInput.Close(); // line added to stop process from hanging on ReadToEnd()

                lblResult.Text = process.StandardOutput.ReadToEnd(); // return CMD.exe output to the Form label here
            }
        }
    }
}
因此,像第一个示例一样抓取新创建的进程的实例非常有效,但是根据ID抓取当前运行的CMD.EXE进程的实例将导致崩溃:

System.dll中发生“System.InvalidOperationException”类型的未处理异常

我知道,通过检查task manager,我有一个PID为22180的cmd.exe正在运行,但是异常发生在:

process.StandardInput.WriteLine(“ping google.com”)


我找到一种写入CMD.EXEs的方法是非常重要的,原因有很多,有人知道我如何让上面的解决方案2工作吗?

完全的例外是什么?使用
ex.ToString()
获取整个内容。似乎是:
System.InvalidOperationException:StandardIn未被重定向。在System.Diagnostics.Process.get_StandardInput()的SendComandToCMD.Form1.button1_单击c:\Users\Matt\Documents\Visual Studio 2013\Projects\SendComandToCMD\SendComandToCMD\Form1.cs中的(对象发送者,事件参数e)第30行
请参见。它说你需要先设置一个属性。