发送cmd命令并在C#中读取结果

时间:2014-07-09 08:54:44

标签: c# process cmd command-line-arguments command-prompt

我想发送带参数的命令并从cmd读取他们的答案。所以,我编写了下面的代码,但它没有工作并锁定在屏幕上(myString通常为null - “”)。我只想将命令发送到打开的命令提示符。问题出在哪儿?提前致谢。 (例如:如何获取ping请求的结果?)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace CallBatchFile
{
    class Program
    {
        [STAThread]
        static void Main()
        {            

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "/c date";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();

            string myString = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
        }
    }
} 

2 个答案:

答案 0 :(得分:1)

cmd /c date正在阻止。你可以使用

p.StartInfo.Arguments = "/c date /T";

停止等待输入的日期,或输入cmd

p.StartInfo.RedirectStandardInput = true;
...
p.StandardInput.Write("\n");

..或读取异步,以便在cmd等待输入时获取输出:

p.BeginOutputReadLine();
p.OutputDataReceived += (_, e) => Console.WriteLine(e.Data);

答案 1 :(得分:0)

此代码可能对您有所帮助

    string strOutput;
    //Starting Information for process like its path, use system shell i.e. control process by system etc.
    ProcessStartInfo psi = new ProcessStartInfo(@"C:\WINDOWS\system32\cmd.exe");
    // its states that system shell will not be used to control the process instead program will handle the process
    psi.UseShellExecute = false;
    psi.ErrorDialog = false;
    // Do not show command prompt window separately
    psi.CreateNoWindow = true;
    psi.WindowStyle = ProcessWindowStyle.Hidden;
    //redirect all standard inout to program
    psi.RedirectStandardError = true;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    //create the process with above infor and start it
    Process plinkProcess = new Process();
    plinkProcess.StartInfo = psi;
    plinkProcess.Start();
    //link the streams to standard inout of process
    StreamWriter inputWriter = plinkProcess.StandardInput;
    StreamReader outputReader = plinkProcess.StandardOutput;
    StreamReader errorReader = plinkProcess.StandardError;
    //send command to cmd prompt and wait for command to execute with thread sleep
    inputWriter.WriteLine("C:\\PLINK -ssh root@susehost -pw opensuselinux echo $SHELL\r\n");
    Thread.Sleep(2000);
    // flush the input stream before sending exit command to end process for any unwanted characters
    inputWriter.Flush();
    inputWriter.WriteLine("exit\r\n");
    // read till end the stream into string
    strOutput = outputReader.ReadToEnd();
    //remove the part of string which is not needed
    int val = strOutput.IndexOf("-type\r\n");
    strOutput = strOutput.Substring(val + 7);
    val = strOutput.IndexOf("\r\n");
    strOutput = strOutput.Substring(0, val);
    MessageBox.Show(strOutput);