Win Command of the Command Line Interface - The Main App (
Page 6 of 7 )
Creating the Main Application
When working with a GUI, you have to open the Program.CS file to begin the process. You see the Main() method, where you change the method to
static void Main(string[] args)
ADVERTISEMENT
Making this modification lets you receive command line input. Now you can begin modifying your application to receive command line input. Listing 4 shows the simple processing used for this example.
Listing 4: Modifying Windows Forms Application to Process the Command Line
static void Main(string[] args)
{
// Display help when requested.
if (args.Contains("/?"))
{
// Display the help screen.
ShowHelp();
return;
}
// Perform the usual startup tasks.
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Holds the greeting.
String TheGreeting = "";
// Check for the /Greet command line switch and obtain the
// greeting if present.
if (args[0].ToUpper() == "/GREET")
TheGreeting = args[1];
// Create the form.
frmMain ThisForm = new frmMain();
// Modify the greeting field.
ThisForm.txtName.Text = TheGreeting;
// Start the application using the new form.
Application.Run(ThisForm);
// This is the old code; comment it out.
//Application.Run(new frmMain());
}
The Windows forms application begins much as it did for a console application. However, notice that there isn’t any debug specific processing needed. This application displays the help information in a dialog box. I’ll show you how to display help at the command line in a few moments.
The original Main() method included three lines of code. Two of these lines come next. You should place these lines after the help processing, but before any other processing, as shown in the example.
The next step is to look for the /Greet command line switch and its accompanying greeting text. The name of the example application is Greetings, so you might type Greetings /Greet "John" and press Enter to automatically add the greeting text to the txtName control. As with the /FD command line switch earlier, you look for the text associated with the /Greet command line switch in the next argument. You also need to enclose the text argument in double quotes as before.
The Main() method normally creates the form as part of the Application.Run() method. However, you want to modify the form before displaying it, so the Main() method must now create the form separately. The controls on the form aren’t accessible normally. To make the controls you want to modify accessible, set the Modifier property to Public. At this point, you can change the value of txtName.Text. The code ends by running the application using the form you created. When the application displays the form, it already has the Your Name field filled in based on the /Greet command line switch.
Performing Console Output with a Windows Forms Application
Believe it or not, you can’t access the console using a Windows forms application unless do something special. A Windows forms application doesn’t have a connection to the console, it can only access the GUI elements you create for it. Fortunately, the fix is amazingly simple. The first step is to add
using System.Runtime.InteropServices;
to your application. Now you can add two functions from the kernel32.dll file as shown in Listing 5.
Listing 5: Interacting with the Console
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeConsole();
static void ShowHelp2()
{
// Attach to the existing application console.
AttachConsole(-1);
// Output the help information.
Console.WriteLine();
Console.WriteLine("Displays a greeting");
Console.WriteLine();
Console.WriteLine("Greetings [/?][/Greet <GreetingString>]");
Console.WriteLine();
Console.WriteLine("/? - Displays the help screen.");
Console.Write("/Greet GreetingString - Provides a greeting to the");
Console.WriteLine(" application.");
// Windows Forms applications insist on adding a readline to the output,
// so we need to display a message.
Console.WriteLine();
Console.WriteLine("Press any key when ready...");
// Detatch from the console.
FreeConsole();
return;
}
To begin the process, you call AttachConsole(-1). This creates a connection from your application to the console. Now you can use Console.WriteLine() calls as normal to write to the console. When you’re finished, call FreeConsole() to remove the connection to the console. That’s all there is to it!
Common Errors to Avoid at the Command Line
The first error that many developers make when creating a command line capable application is to leave out the help. Everyone needs help at the command line. It’s impossible to memorize every command out there. When a user executes your application, he or she will expect to find a /? command line switch. This switch should, by convention, always present the user with application help.
The second error that many developers make is to present information using a GUI. Even Microsoft makes this mistake. Type WScript /? at the command line and press Enter, you see the help as a dialog box, not at the command line as you expect. Supplying help or other information in GUI form is counterintuitive. Even worse, using GUI output prevents the use from using redirection to send the output to a file—a common practice at the command line. If the user is working at the command line, you should provide output at the command line.