show console window from WinForms program - c#

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?

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/

You could output the information using the Console.WriteLine(message) method.

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...

Related

FreeConsole to detach from cmd.exe box with Console Application

I want to change a WinForms application that way, so it will run without UI with console output if it is called with a command line argument. Therefore I changed the application type to "Console Application" and wrote something like:
my code snippet:
....
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
[STAThread]
static void Main(string[] args)
{
if (args.Length == 0)
{
FreeConsole();
Application.Run(new MyForm());
}
else
Console.WriteLine("Console party");
.....
In debug mode in VS I can see the DOS box popping up and hiding. If I do so via console, the command line keeps attached to the process and is not responding until I've closed the MyForm window. Calling the application with an argument, brings the "Console party" up.
The return value of FreeConsole is true and GetLastError is not telling any error code.
Does anybody knows how to detach the process from the cmd.exe process?
Brainstorming with a colleague brings me to try another way. I've set the application type back to Windows Application and did the following:
...
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int input);
static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new MyForm());
}
else if (args.Length == 1)
{
AttachConsole(-1);
Console.WriteLine("Console party");
...
What happens now is, if I run it from cmd.exe box, the application takes the console(stdin,stdou,stderr) from the parent process and writes to it. When using it without arguments, the console window will not pop up.
I am thinking about, what is better way. Having the parent process dealing with the stdin,stdout and stderr handles or let the application hijack it from its parent!?

What is the Purpose of Console.WriteLine() in Winforms

I once saw the source code of a winform application and the code had a Console.WriteLine();. I asked the reason for that and i was told that it was for debugging purposes.
Pls what is the essence of Console.WriteLine(); in a winform and what action does it perform because when i tried using it, it never wrote anything.
It writes to the Console.
The end user won't see it, and to be honest its much cleaner to put it into a proper log, but if you run it through VS the Console window will populate.
Winforms are just console apps that show windows. You can direct your debug info to the console app.
As you can see in the below example there is a command that attaches the parent window then pumps info to it.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MyWinFormsApp
{
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 )
{
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole( ATTACH_PARENT_PROCESS );
// to demonstrate where the console output is going
int argCount = args == null ? 0 : args.Length;
Console.WriteLine( "nYou specified {0} arguments:", argCount );
for (int i = 0; i < argCount; i++)
{
Console.WriteLine( " {0}", args[i] );
}
// launch the WinForms application like normal
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Form1() );
}
}
}
Here is the resource for this example:http://www.csharp411.com/console-output-from-winforms-application/
You wouldn't really use it normally but if you've attached a Console or use AllocConsole, it will function like in any other console application and the output will be visible there.
For quick debugging, I prefer Debug.WriteLine but for a more robust solution, the Trace class may be preferable.
It wouldn't perform anything unless the Console was redirected to say the Output window. Really, they should be leveraging Debug.WriteLine instead.
The benefit of Debug.WriteLine is that it gets optimized away when building in Release mode.
NOTE: as pointed out by Brad Christie and Haedrian, apparently it will in fact write to the Console window in Visual Studio when running a Windows Forms application. You learn something new every day!

winforms output to commandline

I have a winforms application. I would like to run the .exe from the commandline and redirect the output from one of the output textboxes to the commandline. I don't want to launch the winform application just run the logic in the background.
I tried the advice from this thread
C# application both GUI and commandline
but I didn't see any of my console.writeline messages in the command line in cmd.exe when I ran the application via the cmd.exe. Can anyone guide me as to what I might be doing wrong?
I have an if statement that did this logic:
if(args.Length >0)
{
Console.writeline("this has arguments");
new Mainform();
}
else
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
The else part still works. But nothing happens in the if part either using the cmd.exe to run the application or when I use the properties ->debug->command line arguments and give it arguments to run while debugging it. I have no idea what I am doing wrong.
I think you created your Windows Forms project and pasted the code? If so, it couldn't work. You have to create your Console project and here is the code I've tested, works like a charm except that when the Form is shown (GUI mode) the Console is still there, closing that Console will also close your Form:
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else //Process command lines
{
Console.WriteLine("Please enter some command.");
string cmd = Console.ReadLine();
Console.WriteLine("You entered: " + cmd);
}
}
}
Because the Console project doesn't add reference to System.Windows.Forms.dll automatically for you, so you have to do that manually to be able to create your form in that Console project. The point is that you applied the code in a Winforms project while it should be in a Console project. There is only a way I know which can show a Console window in a winforms application is running the cmd.exe but that will show a new Console window than the existing one which you are using to run your application.

Console application closes immediately after opening in visual studio

I am trying to open a console application in visual studio built in C#. As soon as I open it, it closes immediately.
I know windows sets this is a a safety default (atleast I think). How do I fix this?
I know I can compile it and create a shortcut and modify the target so it has the location of the command prompt in it before the applications location. Although the programmer who created this has it generating information into the output of visual studio, so it's imperative that I only open it there.
It happens with most applications and not just in visual studio, just in this case I need it to open in VS 2010. I am using Windows 7.
This is an ancient problem and has inspired several funny cartoons:
Let's fix it. What you want to do is prompt the user to press the Any key when the console app was started from a shortcut on the desktop, Windows Explorer or Visual Studio. But not when it was started from the command processor running its own console. You can do so with a little pinvoke, you can find out if the process is the sole owner of the console window, like this:
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Working on it...");
//...
Console.WriteLine("Done");
PressAnyKey();
}
private static void PressAnyKey() {
if (GetConsoleProcessList(new int[2], 2) <= 1) {
Console.Write("Press any key to continue");
Console.ReadKey();
}
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern int GetConsoleProcessList(int[] buffer, int size);
}
You can also run the application by pressing (Ctrl + F5) .. This will allow it to run in 'Release' mode, and by default, you will need to press 'return' to close the window.
Try adding Console.ReadKey(); at the end of Main() method. This is a quick and dirty way of stopping the window from closing by itself.
You need to wait for user input. Use either Console.ReadLine(), Console.Read(), or Console.ReadKey().
So, according to here
If your process is the only one attached to the console, then the
console will be destroyed when your process exits. If there are other
processes attached to the console, then the console will continue to
exist (because your program won’t be the last one).
And if we adapt the code to C# you would end up something like this:
using System;
using System.Runtime.InteropServices;
namespace CheckIfConsoleWillBeDestroyedAtTheEnd
{
internal class Program
{
private static void Main(string[] args)
{
// ...
if (ConsoleWillBeDestroyedAtTheEnd())
{
Console.WriteLine("Press any key to continue . . .");
Console.ReadKey();
}
}
private static bool ConsoleWillBeDestroyedAtTheEnd()
{
var processList = new uint[1];
var processCount = GetConsoleProcessList(processList, 1);
return processCount == 1;
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern uint GetConsoleProcessList(uint[] processList, uint processCount);
}
}

how to run a winform from console application?

How do I create, execute and control a winform from within a console application?
The easiest option is to start a windows forms project, then change the output-type to Console Application. Alternatively, just add a reference to System.Windows.Forms.dll, and start coding:
using System.Windows.Forms;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form()); // or whatever
}
The important bit is the [STAThread] on your Main() method, required for full COM support.
I recently wanted to do this and found that I was not happy with any of the answers here.
If you follow Marc's advice and set the output-type to Console Application there are two problems:
1) If you launch the application from Explorer, you get an annoying console window behind your Form which doesn't go away until your program exits. We can mitigate this problem by calling FreeConsole prior to showing the GUI (Application.Run). The annoyance here is that the console window still appears. It immediately goes away, but is there for a moment none-the-less.
2) If you launch it from a console, and display a GUI, the console is blocked until the GUI exits. This is because the console (cmd.exe) thinks it should launch Console apps synchronously and Windows apps asynchronously (the unix equivalent of "myprocess &").
If you leave the output-type as Windows Application, but correctly call AttachConsole, you don't get a second console window when invoked from a console and you don't get the unnecessary console when invoked from Explorer. The correct way to call AttachConsole is to pass -1 to it. This causes our process to attach to the console of our parent process (the console window that launched us).
However, this has two different problems:
1) Because the console launches Windows apps in the background, it immediately displays the prompt and allows further input. On the one hand this is good news, the console is not blocked on your GUI app, but in the case where you want to dump output to the console and never show the GUI, your program's output comes after the prompt and no new prompt is displayed when you're done. This looks a bit confusing, not to mention that your "console app" is running in the background and the user is free to execute other commands while it's running.
2) Stream redirection gets messed up as well, e.g. "myapp some parameters > somefile" fails to redirect. The stream redirection problem requires a significant amount of p/Invoke to fixup the standard handles, but it is solvable.
After many hours of hunting and experimenting, I've come to the conclusion that there is no way to do this perfectly. You simply cannot get all the benefits of both console and window without any side effects. It's a matter of picking which side effects are least annoying for your application's purposes.
Here is the best method that I've found:
First, set your projects output type to "Windows Application", then P/Invoke AllocConsole to create a console window.
internal static class NativeMethods
{
[DllImport("kernel32.dll")]
internal static extern Boolean AllocConsole();
}
static class Program
{
static void Main(string[] args) {
if (args.Length == 0) {
// run as windows app
Application.EnableVisualStyles();
Application.Run(new Form1());
} else {
// run as console app
NativeMethods.AllocConsole();
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
It´s very simple to do:
Just add following attribute and code to your Main-method:
[STAThread]
void Main(string[] args])
{
Application.EnableVisualStyles();
//Do some stuff...
while(!Exit)
{
Application.DoEvents(); //Now if you call "form.Show()" your form won´t be frozen
//Do your stuff
}
}
Now you´re fully able to show WinForms :)
You can create a winform project in VS2005/ VS2008 and then change its properties to be a command line application. It can then be started from the command line, but will still open a winform.
All the above answers are great help, but I thought to add some more tips for the absolute beginner.
So, you want to do something with Windows Forms, in a Console Application:
Add a reference to System.Windows.Forms.dll in your Console application project in Solution Explorer. (Right Click on Solution-name->add->Reference...)
Specify the name space in code: using System.Windows.Forms;
Declare the needed properties in your class for the controls you wish to add to the form.
e.g. int Left { get; set; } // need to specify the LEFT position of the button on the Form
And then add the following code snippet in Main():
static void Main(string[] args)
{
Application.EnableVisualStyles();
Form frm = new Form(); // create aForm object
Button btn = new Button()
{
Left = 120,
Width = 130,
Height = 30,
Top = 150,
Text = "Biju Joseph, Redmond, WA"
};
//… more code
frm.Controls.Add(btn); // add button to the Form
// …. add more code here as needed
frm.ShowDialog(); // a modal dialog
}
This worked for my needs...
Task mytask = Task.Run(() =>
{
MyForm form = new MyForm();
form.ShowDialog();
});
This starts the from in a new thread and does not release the thread until the form is closed. Task is in .Net 4 and later.
You should be able to use the Application class in the same way as Winform apps do. Probably the easiest way to start a new project is to do what Marc suggested: create a new Winform project, and then change it in the options to a console application
Its totally depends upon your choice, that how you are implementing.
a. Attached process , ex: input on form and print on console
b. Independent process, ex: start a timer, don't close even if console exit.
for a,
Application.Run(new Form1());
//or -------------
Form1 f = new Form1();
f.ShowDialog();
for b,
Use thread, or task anything,
How to open win form independently?
If you want to escape from Form Freeze and use editing (like text for a button) use this code
Form form = new Form();
Form.Button.Text = "randomText";
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.Run(form);

Categories

Resources