1

I execute a command from my C# App.

It runs fine but sometimes I get errors.

My problem is that I cannot see which is the error or any other text in the command window.

It is just blank.

enter image description here

Is there any way I can make the text show up in the execution time same as it is appeared here?

enter image description here

Here is my code:

        var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
        //processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;
        processInfo.WorkingDirectory = workingFolder;

        var process = Process.Start(processInfo);

        process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => 
        Console.WriteLine("output>>" + e.Data);//MessageBox.Show(e.Data);
        
        process.BeginOutputReadLine();

        process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => 
        Console.WriteLine("error>>" + e.Data);//MessageBox.Show(e.Data);
        process.BeginErrorReadLine();

        process.WaitForExit();

        Console.WriteLine("ExitCode: {0}", process.ExitCode);
        process.Close();
LopDev
  • 823
  • 10
  • 26
asmgx
  • 7,328
  • 15
  • 82
  • 143
  • Your code doesn't read the [StandardOutput](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=netcore-3.1#System_Diagnostics_Process_StandardOutput) or [StandardError](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standarderror?view=netcore-3.1) streams – Panagiotis Kanavos Jul 01 '20 at 07:00
  • 1
    You don't need to use `cmd.exe` to run a process either. Just pass the path to the executable and the arguments to the `ProcessStartInfo` constructor – Panagiotis Kanavos Jul 01 '20 at 07:02

3 Answers3

1

I am not an expert on that but I think you can write this code in a try-catch block and make the exception message be shown on screen using Console.WriteLine() command.

LopDev
  • 823
  • 10
  • 26
Imran
  • 775
  • 7
  • 19
1

In your first screenshot there is Select in the title bar of the cmd window.
Are you aware of the fact that the program gets paused then?
The Select happens if you click in the window and can be continued by pressing Enter (if I remember correctly).
If you use Console.WriteLine() and the cmd window is not paused, you should see what ever you have written.

LopDev
  • 823
  • 10
  • 26
Sven Bardos
  • 872
  • 10
  • 27
1

There is nothing wrong with your code, the problem is that you run your program in the wrong path.

Follow these steps to find the path of your app:

Your app path

Then, in the cmd.exe go to the path with bunch of cd commands.
Here is the code:

var command = "echo hello world"; // < ------ example
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
//processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
        

The output:

output>>hello world
output>>
error>>
ExitCode: 0
Press any key to continue . . .

Also, you can run your app with Ctrl + F5
I know these are so obvious but it's worth to mention them.

Update

You should specify a command, maybe you don't set any command or your command has ~no output~

Update2

I change the code, when the user send args to myapp.exe, it directs it to run.

static void Main(string[] args)
{

    var command = string.Join("", args);
    var processInfo = new ProcessStartInfo("cmd.exe", "/c " +  command);
    ...

Output:

C:\Users\Mahan\Source\Repos\ConsoleApp11\ConsoleApp11\bin\Debug>myapp.exe echo hello

output>>hello world
output>>
error>>
ExitCode: 0
LopDev
  • 823
  • 10
  • 26
Peyman Majidi
  • 1,777
  • 2
  • 18
  • 31