469,575 Members | 1,253 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Console and windows together

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
Nov 22 '05 #1
6 3155
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.
"Max" wrote:
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

Nov 22 '05 #2
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

Max wrote:
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


Nov 22 '05 #3
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.

Nov 22 '05 #4
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

Chris Dunaway wrote:
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.


Nov 22 '05 #5
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" <br*********@yahoo.com> wrote in message news:<11**********************@o13g2000cwo.googleg roups.com>...
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

Max wrote:
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

Nov 22 '05 #6
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

Max wrote:
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


Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by Dom | last post: by
6 posts views Thread by Max | last post: by
6 posts views Thread by Mark Allison | last post: by
12 posts views Thread by Jarod_24 | last post: by
2 posts views Thread by SriBhargav | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.