3

how can i make my console application window to behave like a command prompt window and execute my command line arguments?

6
  • 1
    there are a bunch of resources to be found online for this. Please research before posting here. Also, How do I ask a good question? Commented Apr 23, 2014 at 7:05
  • Google first result for c# console application command line arguments is msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx Commented Apr 23, 2014 at 7:07
  • You both misunderstood the question - read again. Commented Apr 23, 2014 at 7:09
  • What is command prompt command? What is behave like a command prompt window? Do you want to pass command line arguments to your console application or what? Commented Apr 23, 2014 at 7:17
  • 1
    @WimOmbelets I don't think writing a shell wrapper is as trivial as you present it, and it's a question worth asking IMO. Commented Apr 23, 2014 at 7:28

2 Answers 2

2

This should get you started:

public class Program
{
    public static void Main(string[] args)
    {
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName               = "cmd.exe",
                CreateNoWindow         = true,
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true
            }
        };

        proc.Start();

        new Thread(() => ReadOutputThread(proc.StandardOutput)).Start();
        new Thread(() => ReadOutputThread(proc.StandardError)).Start();

        while (true)
        {
            Console.Write(">> ");
            var line = Console.ReadLine();
            proc.StandardInput.WriteLine(line);
        }
    }

    private static void ReadOutputThread(StreamReader streamReader)
    {
        while (true)
        {
            var line = streamReader.ReadLine();
            Console.WriteLine(line);
        }
    }
}

The basics are:

  • open cmd.exe process and capture all three streams (in, out, err)
  • pass input from outside in
  • read output and transfer to your own output.

The "Redirect" options are important - otherwise you can't use the process' respective streams.

The code above is very basic, but you can improve on it.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe you are looking for this

var command = "dir";
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.