14

How can I run a console application in C#, passing parameters to it, and get the result of the application in Unicode? Console.WriteLine is used in the console application. Important point is write Unicode in Console Application.

Sajad Bahmani
  • 17,325
  • 27
  • 86
  • 108
  • Lots of posts. The console only supports 8-bit character encodings. Technically you can switch the Console.OutputEncoding to utf8. That is not going to look good if you ever run it without redirection. Using a file instead would be a good idea. – Hans Passant Jun 29 '11 at 00:07

6 Answers6

20

Sample from MSDN

 // Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();
Jakub Šturc
  • 35,201
  • 25
  • 90
  • 110
5

try with below code, here "Amay" is a argument.

 System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(@"E:\\ConsoleApplicationt\bin\Debug\ConsoleApplicationt.exe", "Amay");

 System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);
Amay Kulkarni
  • 828
  • 13
  • 16
4

Check out Process.Start():

MSDN - Process.Start Method

Your code will probably look something like:

var process = Process.Start(pathToProgram, argsString);

process.WaitForExit();

var exitCode = process.ExitCode;

If by "result of the console application" you mean any output of the program to the console while it runs...you'll need to look at the documentation and figure out how to redirect the output of the program from the console to another stream.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
3

Here http://www.aspcode.net/ProcessStart-and-redirect-standard-output.aspx You can see how to read the output from the console app You start with Process.Start().

Piotr Perak
  • 10,718
  • 9
  • 49
  • 86
1

Take a look at the Process class. You can call any executable using Process.Start("myexe.exe");

eulerfx
  • 36,769
  • 7
  • 61
  • 83
1

You should be careful depending upon your use some of the other examples can have issues. For common mistakes made writing your own code, read "How to use System.Diagnostics.Process correctly"

For a library to use, there is one here: http://csharptest.net/browse/src/Library/Processes with a brief usage guide: "Using the ProcessRunner class"

csharptest.net
  • 62,602
  • 11
  • 71
  • 89