Game Development Reference
In-Depth Information
An even more desirable solution is to build a Windows Forms wrapper around the
console application, and redirect startup parameters and standard input and output.
This solution offers the greatest level of maintainability and speed of development.
If a change happens in the console application, it is immediately accessible by the
Windows Forms version. Additionally, we do not have to worry about the console
application being managed or unmanaged, since the redirection will happen at the
process level, not at the code level.
Implementation
You can use the Process component to start and stop processes and retrieve infor-
mation about the processes currently running on your system. We will be using
this component to launch a console application, specify startup parameters, and
redirect standard input and output. This component exists in the System.Diagnostics
namespace.
using System.Diagnostics;
The following code defines a method that launches a redirected console applica-
tion using the specified file path and argument list. Most of the code is fairly self-
explanatory, though we set UseShellExecute to false so Windows Explorer is not
used to launch the process. RedirectStandardInput is set to true so that we can get
a stream handle to the console output. We also set CreateNoWindow to true so that a
command prompt window is not launched alongside our Windows Forms appli-
cation when the process is started.
The StandardOutput property returns a StreamReader that can retrieve the output
data from the console application. The HasExited property can be queried while
data is being read from the output stream.
public void LaunchConsoleApplication(string fileName, string arguments)
{
if (!File.Exists(fileName))
{
MessageBox.Show(“Invalid path to console application!”);
return;
}
Process process = new Process();
process.StartInfo.FileName = fileName;
process.StartInfo.UseShellExecute = false;
Search Nedrilad ::




Custom Search