Reputation: 63
I have a WinForms program. If a user starts it from the command line and passes an invalid parameter, how could I display error information in the console window?
Upvotes: 1
Views: 865
Reputation: 8792
Your WinForms app could raise a pop up, like a message box, that shows the error.
If the application is running in unattended mode as part of a batch file, you could write to the Event Log, or to a custom log file created by the application.
The other answers that tell you to use Console.WriteLine are also great if your batch command redirects output to a file, such as...
myApp.exe >> errors.txt
this appends the console to the error.txt file...
Upvotes: 0
Reputation: 11820
Here is example how to display message to command line if bad parameter is passed to winform application:
static class Program
{
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//if parameter -window is passed - opens main form, else displays Bad params message
if(args[0] == "-window")
Application.Run(new main());
else
{
//Attach console process
AttachConsole(ATTACH_PARENT_PROCESS);
Console.WriteLine("Bad params");
}
}
}
Reference: http://www.csharp411.com/console-output-from-winforms-application/
Upvotes: 4
Reputation: 696
You could output the information using the Console.WriteLine(message) method.
Upvotes: 1