For example, write C:\app.exe /h Enter and get the response "Command completed."
Windows Form - C # (Program.cs file)
For example, write C:\app.exe /h Enter and get the response "Command completed."
Windows Form - C # (Program.cs file)
There are several options for this:
create a new console:
using System; using System.Runtime.InteropServices; using System.Windows.Forms; using WindowsFormsApp2; static class Program { [STAThread] static void Main() { AllocConsole(); Console.WriteLine("Hello"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool AllocConsole(); } priattachitsya to the console:
public class GUIConsoleWriter { private const int ATTACH_PARENT_PROCESS = -1; StreamWriter _stdOutWriter; public GUIConsoleWriter() { var stdout = Console.OpenStandardOutput(); _stdOutWriter = new StreamWriter(stdout); _stdOutWriter.AutoFlush = true; AttachConsole(ATTACH_PARENT_PROCESS); } [DllImport("kernel32.dll")] private static extern bool AttachConsole(int dwProcessId); } Program.cs:
static class Program { [STAThread] private static void Main() { var consoleWriter = new GUIConsoleWriter(); Console.WriteLine("Hello"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } That is, if you start your application from an already running console, or with Far Manager, then to get the result in their output window - use the second option. The first if the console needs to be created.
Source: SO
PS: do not forget to thank the authors of the original answers =)
Console.WriteLine("Hello"); which output Hello to the console, if you need something your output, change its text. I used this for the test. - Anton KomyshanIn .NET, the process.Start static method is used to start a new process, which returns an instance of the Process class. This class has a property that provides access to a stream to which the program can display messages: StandardOutput . Access is not given directly, but through StreamReader , which is even more convenient. In order for the program to output the result not to the console, but to StandardOutput, it is necessary in the process properties ( ProcessStartInfo ) to turn on the RedirectStandardOutput option.
If you are faced with the task to launch a console application from your program and read what it brings to the console, use the Process.Start method to start the application, and use the StreamReader obtained from StandardOutput to read the result. For example, you can read all the data that a stream contains in text format using the ReadToEnd method.
But there is a nuance. You can not immediately take and read the answer, because the process takes some time to complete the work and output the result to the console. The ReadToEnd method will wait for the process to complete. If it is necessary for your program to continue to function while waiting (for example, to redraw the window, respond to user actions), use the ReadToEndAsync method, which will call the delegate on completion and pass it the result, or the ReadLine method, which returns control as the lines are received, rather than waiting the end of the process.
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\app.exe", @"/h"); // можно даже скрыть окно запущенного процесса startInfo.WindowStyle = ProcessWindowStyle.Hidden; // указываем что программа должна выводить резульат в поток привязанный к свойству StandardOutput startInfo.RedirectStandardOutput = true; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; // запускаем процесс Process procCommand = Process.Start(startInfo); // получаем ответ запущенного процесса StreamReader srIncoming = procCommand.StandardOutput; string result = srIncoming.ReadToEnd(); An example is taken and revised from this article: Running the command line and getting a response .
The code is like this (I hope there are no errors in it):
using System; using System.Linq; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var consoleWriter = new GUIConsoleWriter(); var form = new Form1(); bool needRun = true; foreach (string arg in args) { switch (arg) { case "/h": Console.WriteLine("Команда выполнена"); form.Check1(); needRun = false; break; case "/s": Console.WriteLine("Команда выполнена"); form.Check2(); needRun = false; break; case "/e": Console.WriteLine("Команда выполнена"); form.Check3(); needRun = false; break; } } if (needRun) { Application.Run(form); } } public class GUIConsoleWriter { private const int ATTACH_PARENT_PROCESS = -1; StreamWriter _stdOutWriter; public GUIConsoleWriter() { var stdout = Console.OpenStandardOutput(); _stdOutWriter = new StreamWriter(stdout); _stdOutWriter.AutoFlush = true; AttachConsole(ATTACH_PARENT_PROCESS); } [DllImport("kernel32.dll")] private static extern bool AttachConsole(int dwProcessId); } Found another easier way:
using System; using System.Linq; using System.Windows.Forms; using System.Runtime.InteropServices; static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var form = new Form1(); if (AttachConsole(-1)) { foreach (string arg in args) { switch (arg) { case "/h": form.Check1(); Console.Write("\r\n" + "Команда /h выполнена"); break; case "/s": form.Check2(); Console.Write("\r\n" + "Команда /s выполнена"); break; case "/e": form.Check3(); Console.Write("\r\n" + "Команда /e выполнена"); break; } } } else { Application.Run(form); } } [DllImport("kernel32.dll")] private static extern bool AttachConsole(int procid); Source: link
Source: https://ru.stackoverflow.com/questions/626427/
All Articles
Console.WriteLine? - Zufir