Console and windows together

M

Max

Hi All,

I am developing an application that needs to run in one of two modes:
1. If No command line param is provided the application should run as
Window form.
2. If command line param is provided the application should run as a
console application.

For this i created a windows application with Main

static void Main(string[] cmdLine)
{
Form1 frm = new Form1();
if(cmdLine.Length == 0)
{
frm.InitializeForm();
Application.Run(frm);
}
else
{
frm.ProcessCommandline(cmdLine);
}
}

Problem i have is "Console.writeline" does not work while running the
application as console ie running the application with params.

Pls help

thanks in advance

regards

Max
 
G

Guest

You will probably have to redirect the output of the Console using the
Console.SetOut and Console.OpenStandardOutput method (check the docs for this
method).

HTH, Jakob.
 
B

Brian Gideon

Max,

[DllImport("kernel32.dll")]
public static extern bool AllocConsole();

[DllImport("kernel32.dll")]
public static extern bool FreeConsole();

public static void Main(string[] args)
{
if (args.Length > 0)
{
AllocConsole();
try
{
// Place console version of code here.
}
finally
{
FreeConsole();
}
}
else
{
// Place windows version of code here.
}
}

There's also an AttachConsole API that you might want to play around
with.

Brian
 
C

Chris Dunaway

What type of project did you create? Try creating a console
application and then add a reference to the System.Windows.Forms.dll.

You can then use code like this: (vb.Net)

Sub Main(ByVal args() As String)

If args.Length > 0 Then
Console.WriteLine("Started with command line args")
Else
Application.Run(New Form1)
End If

End Sub

Worked ok for me.
 
B

Brian Gideon

Chris,

That works too. The only problem I have with this approach is that a
console window is always created. It may not be a big deal for the OP
though. In that case it is certainly easier than using console APIs.

Brian
 
M

Max

Brain,

I did exactly the same, but while running the application under cmd in
XP,
a new console window fashes. ie. a console window gets created and
exits without displaying any messages.

And i tried what Chris suggested, as you said a console window gets
created when running the app without any arguments.

Any more suggesions.

Max

Brian Gideon said:
Max,

[DllImport("kernel32.dll")]
public static extern bool AllocConsole();

[DllImport("kernel32.dll")]
public static extern bool FreeConsole();

public static void Main(string[] args)
{
if (args.Length > 0)
{
AllocConsole();
try
{
// Place console version of code here.
}
finally
{
FreeConsole();
}
}
else
{
// Place windows version of code here.
}
}

There's also an AttachConsole API that you might want to play around
with.

Brian
Hi All,

I am developing an application that needs to run in one of two modes:
1. If No command line param is provided the application should run as
Window form.
2. If command line param is provided the application should run as a
console application.

For this i created a windows application with Main

static void Main(string[] cmdLine)
{
Form1 frm = new Form1();
if(cmdLine.Length == 0)
{
frm.InitializeForm();
Application.Run(frm);
}
else
{
frm.ProcessCommandline(cmdLine);
}
}

Problem i have is "Console.writeline" does not work while running the
application as console ie running the application with params.

Pls help

thanks in advance

regards

Max
 
B

Brian Gideon

Max,

Make sure the project output type is "Windows Application". If you run
the application in the debugger it will create a new console window,
but the standard output is redirected to the output window in the IDE
and not the new console window. Look there for the output. If you run
the application from cmd it will create a new console and send the
standard output there. Be sure to put a Console.ReadLine call at the
end so that you have time to read the output. If you want the standard
output to go the command prompt directly you may have to use the
AttachConsole API. I haven't experimented around with that yet.

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top