473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3316
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Dom | last post by:
can someone please help me display text in the console cout << "Testing"; in a different colour to the default one
6
by: Max | last post by:
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...
6
by: Mark Allison | last post by:
Hi, I have an application that I want to be to run in Console mode and GUI mode. If no params are entered, I want the GUI fired up, if params are entered, then go into console mode. I believe...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
12
by: Jarod_24 | last post by:
I got a project called "Forms" that hold some forms and stuff in my solution. A argument at startup defines wether to use a From or Console. My plan was to create a myConsole class that would...
8
by: Alison | last post by:
Hi, Al I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
6
by: Ant | last post by:
Hi all, I'm putting together a simple help module for my applications, using html files stored in the application directory somewhere. Basically it sets up a basic web server, and then uses the...
2
by: SriBhargav | last post by:
Hi, I've a question on setting timeout on console.readline() I would like the user to input something through Console.readline() in 5 secs. If there is no input in that time, I would like to...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.