So the program I'm working on can be launched using command lines in CMD using the following code.
string[] commandLines = Environment.GetCommandLineArgs();
However I want to be able to return a message to the CMD window where the command lines came from after handling them. Any help would be appreciated.
Edit: I'm running the program as a Windows Application, not a console application.
To display the command prompt, type echo on. If used in a batch file, echo on and echo off don't affect the setting at the command prompt. To prevent echoing a particular command in a batch file, insert an @ sign in front of the command.
Note: Be careful to change the active directory for the command prompt before running the command. This way you'll know where the output files are stored. You can view the standard output that went to the file by typing “myoutput. txt” in the command window.
In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.
The echo command repeats typed text back to the screen and can send text to a peripheral on the computer, such as a COM port.
I ended up solving the problem by using one of the answers RenniePet posted as a comment to my question. I'll list the solution down here for anyone trying to reproduce it.
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
StreamWriter _stdOutWriter;
// this must be called early in the program
public void GUIConsoleWriter()
{
// this needs to happen before attachconsole.
// If the output is not redirected we still get a valid stream but it doesn't appear to write anywhere
// I guess it probably does write somewhere, but nowhere I can find out about
var stdout = Console.OpenStandardOutput();
_stdOutWriter = new StreamWriter(stdout);
_stdOutWriter.AutoFlush = true;
AttachConsole(ATTACH_PARENT_PROCESS);
}
public void WriteLine(string line)
{
GUIConsoleWriter();
_stdOutWriter.WriteLine(line);
Console.WriteLine(line);
}
After you've added this code to your program you can simply start returning lines by using, for example, the following.
WriteLine("\nExecuting commands.");
You can use the .NET SendKeys class to send keystrokes to an application you do not own. The target application must be active to be able to retrieve the keystrokes. Therefore before sending you have to activate your target application. You do so by getting a handle of the window and pinvoking into SetForegroundWindow with your handle.
Here is some example code to get you started:
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lp1, string lp2);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
IntPtr handle = FindWindow("ConsoleWindowClass", "Eingabeaufforderung");
if (!handle.Equals(IntPtr.Zero))
{
if (SetForegroundWindow(handle))
{
// send
SendKeys.Send("Greetings from Postlagerkarte!");
// send key "Enter"
SendKeys.Send("{ENTER}");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With