JoshuaJeanThree
JoshuaJeanThree

Reputation: 1392

Sending cmd commands and reading results in C#

I want to send commands with arguments and read their answers from cmd. So, I wrote the code below, but it is not working and locks on screen (myString is usually null - ""). I only want to send commands to an opened command prompt. Where is the problem? Thanks in advance. (for example: How can I fetch the result of a ping request?)

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();
        }
    }
} 

Upvotes: 2

Views: 2103

Answers (2)

Novice
Novice

Reputation: 570

This code might help you

    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);

Upvotes: 0

mtmk
mtmk

Reputation: 6326

cmd /c date is blocking. you can either use

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

To stop date waiting for input, or give input to cmd

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

..or read async so you can get the output while cmd is waiting for your input:

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

Upvotes: 1

Related Questions