473,394 Members | 1,699 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,394 software developers and data experts.

GUI and Console Apps

Hi I have a really basic question hopefully some can help me with:

Can you write a (i.e. one) C# project that works from the cmd line and gui?

I seems if i write a GUI app it can't write to console using
System.Console.WriteLine if thge project has its "Output Type" to
"Windows Application".

However I can write to stdio if i set output type to "Console
Application". When I do this I unfortunately get a "console box" as well
as the UI when i start the UI without command line instructions.

Cheers,
JIm
Nov 17 '05 #1
17 4180


Surely if you want to write to the console then you'd want to see the
console box as you want to write to it?

or do you only want to see the console when it's run from cmd? (in which
case, compile as GUI, run from cmd)

I think I'm not understanding what you want.... can you explain more?

"MumboJumbo" wrote:
Hi I have a really basic question hopefully some can help me with:

Can you write a (i.e. one) C# project that works from the cmd line and gui?

I seems if i write a GUI app it can't write to console using
System.Console.WriteLine if thge project has its "Output Type" to
"Windows Application".

However I can write to stdio if i set output type to "Console
Application". When I do this I unfortunately get a "console box" as well
as the UI when i start the UI without command line instructions.

Cheers,
JIm

Nov 17 '05 #2


Surely if you want to write to the console then you'd want to see the
console box as you want to write to it?

or do you only want to see the console when it's run from cmd? (in which
case, compile as GUI, run from cmd)

I think I'm not understanding what you want.... can you explain more?

"MumboJumbo" wrote:
Hi I have a really basic question hopefully some can help me with:

Can you write a (i.e. one) C# project that works from the cmd line and gui?

I seems if i write a GUI app it can't write to console using
System.Console.WriteLine if thge project has its "Output Type" to
"Windows Application".

However I can write to stdio if i set output type to "Console
Application". When I do this I unfortunately get a "console box" as well
as the UI when i start the UI without command line instructions.

Cheers,
JIm

Nov 17 '05 #3
I want an app that will do this:

cmd.exe ... start the gui with a new workspace
cmd.exe file.xml ... start the gui with file.xml

But when a certain group of extra args are used it will run as a console
app ... -applytransform junk.xlst file.xml ... etc ...
Jim

Chris Walters wrote:

Surely if you want to write to the console then you'd want to see the
console box as you want to write to it?

or do you only want to see the console when it's run from cmd? (in which
case, compile as GUI, run from cmd)

I think I'm not understanding what you want.... can you explain more?

"MumboJumbo" wrote:

Hi I have a really basic question hopefully some can help me with:

Can you write a (i.e. one) C# project that works from the cmd line and gui?

I seems if i write a GUI app it can't write to console using
System.Console.WriteLine if thge project has its "Output Type" to
"Windows Application".

However I can write to stdio if i set output type to "Console
Application". When I do this I unfortunately get a "console box" as well
as the UI when i start the UI without command line instructions.

Cheers,
JIm

Nov 17 '05 #4
"or do you only want to see the console when it's run from cmd? (in which
case, compile as GUI, run from cmd)"

Sorry, thats clearly not true - what was I thinking!

I think my question back to you is; Why do you want to write to the console
when the application is a GUI?

If for debugging only --> System.Console writes to the output window of
Visual Studio, so you'll see it when developing / debugging etc.

If for other reasons, maybe write to a log file on disk? Maybe only create
the log file when the App.config has a "log=on" flag set - allow it to be
turned on / off ?

Nov 17 '05 #5
I want an app that will do this:

cmd.exe ... start the gui with a new workspace
cmd.exe file.xml ... start the gui with file.xml

But when a certain group of extra args are used it will run as a console
app ... -applytransform junk.xlst file.xml ... etc ...
Jim

Chris Walters wrote:

Surely if you want to write to the console then you'd want to see the
console box as you want to write to it?

or do you only want to see the console when it's run from cmd? (in which
case, compile as GUI, run from cmd)

I think I'm not understanding what you want.... can you explain more?

"MumboJumbo" wrote:

Hi I have a really basic question hopefully some can help me with:

Can you write a (i.e. one) C# project that works from the cmd line and gui?

I seems if i write a GUI app it can't write to console using
System.Console.WriteLine if thge project has its "Output Type" to
"Windows Application".

However I can write to stdio if i set output type to "Console
Application". When I do this I unfortunately get a "console box" as well
as the UI when i start the UI without command line instructions.

Cheers,
JIm

Nov 17 '05 #6
"or do you only want to see the console when it's run from cmd? (in which
case, compile as GUI, run from cmd)"

Sorry, thats clearly not true - what was I thinking!

I think my question back to you is; Why do you want to write to the console
when the application is a GUI?

If for debugging only --> System.Console writes to the output window of
Visual Studio, so you'll see it when developing / debugging etc.

If for other reasons, maybe write to a log file on disk? Maybe only create
the log file when the App.config has a "log=on" flag set - allow it to be
turned on / off ?

Nov 17 '05 #7
Compile as GUI --> you can still start it from the command line.

make your main like this:

static void Main(string[] args) {
//do argument "magic" here:
if (args!=null){
string firstParameter = args[0];
}
}

--> Check this for some nice code to pull out your parameters:
http://www.codeproject.com/csharp/Cp...ineEmailer.asp
//take a look at Main and CommandLineParser(string sCommandLine)

Assuming you don't want to run in a batch environment - I'd personally not
bother with the command line parameters, and use the App.config to push
parameters into the application. If you are running in batch - don't do a GUI
!

If the parameters change frequently, maybe have the ability to store them in
a file somewhere and then read that file in.

If you want to kick it off from the command line and then have it auto kill
the "black window" - I'm not sure this is possible....

Check out using App.config and look into
System.Configuration.ConfigurationSettings.AppSett ings
Nov 17 '05 #8
Compile as GUI --> you can still start it from the command line.

make your main like this:

static void Main(string[] args) {
//do argument "magic" here:
if (args!=null){
string firstParameter = args[0];
}
}

--> Check this for some nice code to pull out your parameters:
http://www.codeproject.com/csharp/Cp...ineEmailer.asp
//take a look at Main and CommandLineParser(string sCommandLine)

Assuming you don't want to run in a batch environment - I'd personally not
bother with the command line parameters, and use the App.config to push
parameters into the application. If you are running in batch - don't do a GUI
!

If the parameters change frequently, maybe have the ability to store them in
a file somewhere and then read that file in.

If you want to kick it off from the command line and then have it auto kill
the "black window" - I'm not sure this is possible....

Check out using App.config and look into
System.Configuration.ConfigurationSettings.AppSett ings
Nov 17 '05 #9
A common pattern to achieve this is:
- Create two apps. One gui and one console by the same name and put them in
the same directory.
- Name the console version *.com and the gui *.exe. The .com will be found
first in the normal search order so you console app will start first.
- Your console app will parse the command line. If console args, the app
continues as your normal.
- If args dictate GUI args, you Process start the *.exe command in same dir
passing in the args.

This will allow your console version to use the parent console as you would
expect (i.e. no new console). And will also allow you to start the GUI
version based on args and leave the current console intact. A desktop icon
or start menu would point the exe version if you wanted gui. From console
you could also specify *.exe if you wanted direct access to the gui version.
hth

--
William Stacey [MVP]

"MumboJumbo" <ju**@junk.be> wrote in message
news:Xa*******************@news20.bellglobal.com.. .
Hi I have a really basic question hopefully some can help me with:

Can you write a (i.e. one) C# project that works from the cmd line and
gui?

I seems if i write a GUI app it can't write to console using
System.Console.WriteLine if thge project has its "Output Type" to "Windows
Application".

However I can write to stdio if i set output type to "Console
Application". When I do this I unfortunately get a "console box" as well
as the UI when i start the UI without command line instructions.

Cheers,
JIm

Nov 17 '05 #10
A common pattern to achieve this is:
- Create two apps. One gui and one console by the same name and put them in
the same directory.
- Name the console version *.com and the gui *.exe. The .com will be found
first in the normal search order so you console app will start first.
- Your console app will parse the command line. If console args, the app
continues as your normal.
- If args dictate GUI args, you Process start the *.exe command in same dir
passing in the args.

This will allow your console version to use the parent console as you would
expect (i.e. no new console). And will also allow you to start the GUI
version based on args and leave the current console intact. A desktop icon
or start menu would point the exe version if you wanted gui. From console
you could also specify *.exe if you wanted direct access to the gui version.
hth

--
William Stacey [MVP]

"MumboJumbo" <ju**@junk.be> wrote in message
news:Xa*******************@news20.bellglobal.com.. .
Hi I have a really basic question hopefully some can help me with:

Can you write a (i.e. one) C# project that works from the cmd line and
gui?

I seems if i write a GUI app it can't write to console using
System.Console.WriteLine if thge project has its "Output Type" to "Windows
Application".

However I can write to stdio if i set output type to "Console
Application". When I do this I unfortunately get a "console box" as well
as the UI when i start the UI without command line instructions.

Cheers,
JIm

Nov 17 '05 #11
Sorry I have a requirement from my boss that:

1.) i have 1 executable ...
2.) this one excutable will start in GUI mode when started with no args ...
3.) and start in CLI mode when there is args ...
4.) The CLI has to be executable from with in a makefile and should be
seemless ...
The GUI code is nearly 3 years old ... and now I need CLI functioanality
....

Thanks again,
Jim

Chris Walters wrote:
"or do you only want to see the console when it's run from cmd? (in which
case, compile as GUI, run from cmd)"

Sorry, thats clearly not true - what was I thinking!

I think my question back to you is; Why do you want to write to the console
when the application is a GUI?

If for debugging only --> System.Console writes to the output window of
Visual Studio, so you'll see it when developing / debugging etc.

If for other reasons, maybe write to a log file on disk? Maybe only create
the log file when the App.config has a "log=on" flag set - allow it to be
turned on / off ?

Nov 17 '05 #12
Thank you ... this is what i was trying to get at ...

So you must have two apps if you would like "typical" console
interaction ...

Thanks,
Jim

William Stacey [MVP] wrote:
A common pattern to achieve this is:
- Create two apps. One gui and one console by the same name and put them in
the same directory.
- Name the console version *.com and the gui *.exe. The .com will be found
first in the normal search order so you console app will start first.
- Your console app will parse the command line. If console args, the app
continues as your normal.
- If args dictate GUI args, you Process start the *.exe command in same dir
passing in the args.

This will allow your console version to use the parent console as you would
expect (i.e. no new console). And will also allow you to start the GUI
version based on args and leave the current console intact. A desktop icon
or start menu would point the exe version if you wanted gui. From console
you could also specify *.exe if you wanted direct access to the gui version.
hth

Nov 17 '05 #13
Hi All,

I really thought, there would be some help by the experts I am not of this
type], but I have stored some - for me importent - resoures.

Try the code [C#] at the end, it works for me.

Hope, this helps,
Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

================================================== =================

/*
Compile: csc /t:exe /out:DualTest2.exe DualTest2.cs
from http://www.dotnet247.com/247referenc.../17/88693.aspx
Other console-realted code was published by Matthias Sjögren.
*/

using System;
using System.IO;
using System.Diagnostics;
using System.Configuration;
using System.Runtime.InteropServices; //Needed for WIN32API-calls.
using System.Threading;
using System.Windows.Forms;
class DualTest
{
private static string strAppName = "DualTest";
public static bool useWindow = false;
public static Form Form1 = new Form();
public static int intFreeConsoleRTC;

static int Main(string[] args)
{
//Read application specific settings:
try
{
useWindow =
bool.Parse(ConfigurationSettings.AppSettings["AlwaysUseWindow"]);
}
catch(System.Exception)
{}

if( (args.Length == 0) || (useWindow) )
{
Console.WriteLine("No args, using a window...");
intFreeConsoleRTC = FreeConsole();
if(intFreeConsoleRTC != 0)
{
try
{
/*
The Thread.Sleep() demonstrates, that the console is detached. If one
starts this proggi with a doubleclick in windows explorer, first,
a console window opens. After calling "FreeConsole()", the
console window disappears. The delay is made, to show this effect,
because opening the form is delayed.
*/
Thread.Sleep(2500);

//First method, de-commented now:
Application.Run(Form1);

//This could also be used!
//System.Windows.Forms.MessageBox.Show("Hello, world!");
}
catch(Exception e)
{
//Do something here
}
}
else
{
//The console was NOT detached for some reasons, we can continue writing
there...
Console.WriteLine("Console was NOT freed, reason:" +
intFreeConsoleRTC );
}
return 1;
}
else
{
//We use the existing arguments and proceed...
Console.WriteLine(strAppName + ", have all, NOT using a window.");

return 0;
}
}
[DllImport("kernel32.dll")]
private static extern int FreeConsole(); //BOOL FreeConsole(VOID);
}

================================================== =================
"MumboJumbo" <ju**@junk.be> wrote in message
news:1R*********************@news20.bellglobal.com ...
Thank you ... this is what i was trying to get at ...

So you must have two apps if you would like "typical" console
interaction ...

Thanks,
Jim

William Stacey [MVP] wrote:
A common pattern to achieve this is:
- Create two apps. One gui and one console by the same name and put them in the same directory.
- Name the console version *.com and the gui *.exe. The .com will be found first in the normal search order so you console app will start first.
- Your console app will parse the command line. If console args, the app continues as your normal.
- If args dictate GUI args, you Process start the *.exe command in same dir passing in the args.

This will allow your console version to use the parent console as you would expect (i.e. no new console). And will also allow you to start the GUI
version based on args and leave the current console intact. A desktop icon or start menu would point the exe version if you wanted gui. From console you could also specify *.exe if you wanted direct access to the gui version. hth

Nov 17 '05 #14
Yes. The best I have been able to find over the years. MS also uses this
pattern for many of their tools that have both gui and console versions. I
am all ears if anyone has a better way.

--
William Stacey [MVP]

"MumboJumbo" <ju**@junk.be> wrote in message
news:1R*********************@news20.bellglobal.com ...
Thank you ... this is what i was trying to get at ...

So you must have two apps if you would like "typical" console interaction
...

Thanks,
Jim

William Stacey [MVP] wrote:
A common pattern to achieve this is:
- Create two apps. One gui and one console by the same name and put them
in the same directory.
- Name the console version *.com and the gui *.exe. The .com will be
found first in the normal search order so you console app will start
first.
- Your console app will parse the command line. If console args, the app
continues as your normal.
- If args dictate GUI args, you Process start the *.exe command in same
dir passing in the args.

This will allow your console version to use the parent console as you
would expect (i.e. no new console). And will also allow you to start the
GUI version based on args and leave the current console intact. A
desktop icon or start menu would point the exe version if you wanted gui.
From console you could also specify *.exe if you wanted direct access to
the gui version. hth

Nov 17 '05 #15
Thats perfect!!!!!!!!
Thank you soooo much ...
Jim

Manfred Braun wrote:
Hi All,

I really thought, there would be some help by the experts I am not of this
type], but I have stored some - for me importent - resoures.

Try the code [C#] at the end, it works for me.

Hope, this helps,
Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

================================================== =================

/*
Compile: csc /t:exe /out:DualTest2.exe DualTest2.cs
from http://www.dotnet247.com/247referenc.../17/88693.aspx
Other console-realted code was published by Matthias Sjögren.
*/

using System;
using System.IO;
using System.Diagnostics;
using System.Configuration;
using System.Runtime.InteropServices; //Needed for WIN32API-calls.
using System.Threading;
using System.Windows.Forms;
class DualTest
{
private static string strAppName = "DualTest";
public static bool useWindow = false;
public static Form Form1 = new Form();
public static int intFreeConsoleRTC;

static int Main(string[] args)
{
//Read application specific settings:
try
{
useWindow =
bool.Parse(ConfigurationSettings.AppSettings["AlwaysUseWindow"]);
}
catch(System.Exception)
{}

if( (args.Length == 0) || (useWindow) )
{
Console.WriteLine("No args, using a window...");
intFreeConsoleRTC = FreeConsole();
if(intFreeConsoleRTC != 0)
{
try
{
/*
The Thread.Sleep() demonstrates, that the console is detached. If one
starts this proggi with a doubleclick in windows explorer, first,
a console window opens. After calling "FreeConsole()", the
console window disappears. The delay is made, to show this effect,
because opening the form is delayed.
*/
Thread.Sleep(2500);

//First method, de-commented now:
Application.Run(Form1);

//This could also be used!
//System.Windows.Forms.MessageBox.Show("Hello, world!");
}
catch(Exception e)
{
//Do something here
}
}
else
{
//The console was NOT detached for some reasons, we can continue writing
there...
Console.WriteLine("Console was NOT freed, reason:" +
intFreeConsoleRTC );
}
return 1;
}
else
{
//We use the existing arguments and proceed...
Console.WriteLine(strAppName + ", have all, NOT using a window.");

return 0;
}
}
[DllImport("kernel32.dll")]
private static extern int FreeConsole(); //BOOL FreeConsole(VOID);
}

================================================== =================
"MumboJumbo" <ju**@junk.be> wrote in message
news:1R*********************@news20.bellglobal.com ...
Thank you ... this is what i was trying to get at ...

So you must have two apps if you would like "typical" console
interaction ...

Thanks,
Jim

William Stacey [MVP] wrote:

A common pattern to achieve this is:
- Create two apps. One gui and one console by the same name and put
them in
the same directory.
- Name the console version *.com and the gui *.exe. The .com will be
found
first in the normal search order so you console app will start first.
- Your console app will parse the command line. If console args, the
app
continues as your normal.
- If args dictate GUI args, you Process start the *.exe command in same
dir
passing in the args.

This will allow your console version to use the parent console as you
would
expect (i.e. no new console). And will also allow you to start the GUI
version based on args and leave the current console intact. A desktop
icon
or start menu would point the exe version if you wanted gui. From
console
you could also specify *.exe if you wanted direct access to the gui
version.
hth


Nov 17 '05 #16
I found this code in german online ... it prevents one problem with that
last code posting: "the exec crashes if you call
system.console.writeline()" ...

Jim

[DllImport("Kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);

/// <summary>->Set to true, if the Application should run again
/// even after closing. This is used for setting a new Language.</summary>
public static bool RunAgain=false;

private const Int32 SW_HIDE = 0;

/// <summary>The main entry point for the application.</summary>
[STAThread]
static void Main()
{
try {IntPtr hwnd = GetConsoleWindow();
if (hwnd!=IntPtr.Zero) ShowWindow(hwnd, SW_HIDE);}
catch {}
AnzArgs= Environment.GetCommandLineArgs().Length;
Args = Environment.GetCommandLineArgs();
if (AnzArgs != 1) // Console
{
Console.WriteLine(Application.ProductName+ " started.");
Application.Run(new Form1());
Console.WriteLine(Application.ProductName+ " finished."));
return;
}
do {Application.Run(new Form1());} while (RunAgain);
// Letzteres ist für dynamische Sprach-Umschaltung.

}

MumboJumbo wrote:
Thats perfect!!!!!!!!
Thank you soooo much ...
Jim

Manfred Braun wrote:
Hi All,

I really thought, there would be some help by the experts I am not of
this
type], but I have stored some - for me importent - resoures.

Try the code [C#] at the end, it works for me.

Hope, this helps,
Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

================================================== =================

/*
Compile: csc /t:exe /out:DualTest2.exe DualTest2.cs
from http://www.dotnet247.com/247referenc.../17/88693.aspx
Other console-realted code was published by Matthias Sjögren.
*/

using System;
using System.IO;
using System.Diagnostics;
using System.Configuration;
using System.Runtime.InteropServices; //Needed for WIN32API-calls.
using System.Threading;
using System.Windows.Forms;
class DualTest
{
private static string strAppName = "DualTest";
public static bool useWindow = false;
public static Form Form1 = new Form();
public static int intFreeConsoleRTC;

static int Main(string[] args)
{
//Read application specific settings:
try
{
useWindow =
bool.Parse(ConfigurationSettings.AppSettings["AlwaysUseWindow"]);
}
catch(System.Exception)
{}

if( (args.Length == 0) || (useWindow) )
{
Console.WriteLine("No args, using a window...");
intFreeConsoleRTC = FreeConsole();
if(intFreeConsoleRTC != 0)
{
try
{
/*
The Thread.Sleep() demonstrates, that the console is detached. If
one
starts this proggi with a doubleclick in windows explorer, first,
a console window opens. After calling "FreeConsole()", the
console window disappears. The delay is made, to show this effect,
because opening the form is delayed.
*/
Thread.Sleep(2500);

//First method, de-commented now:
Application.Run(Form1);

//This could also be used!
//System.Windows.Forms.MessageBox.Show("Hello, world!");
}
catch(Exception e)
{
//Do something here
}
}
else
{
//The console was NOT detached for some reasons, we can continue
writing
there...
Console.WriteLine("Console was NOT freed, reason:" +
intFreeConsoleRTC );
}
return 1;
}
else
{
//We use the existing arguments and proceed...
Console.WriteLine(strAppName + ", have all, NOT using a window.");

return 0;
}
}
[DllImport("kernel32.dll")]
private static extern int FreeConsole(); //BOOL FreeConsole(VOID);
}

================================================== =================
"MumboJumbo" <ju**@junk.be> wrote in message
news:1R*********************@news20.bellglobal.com ...
Thank you ... this is what i was trying to get at ...

So you must have two apps if you would like "typical" console
interaction ...

Thanks,
Jim

William Stacey [MVP] wrote:
A common pattern to achieve this is:
- Create two apps. One gui and one console by the same name and put

them in
the same directory.
- Name the console version *.com and the gui *.exe. The .com will be

found
first in the normal search order so you console app will start first.
- Your console app will parse the command line. If console args, the

app
continues as your normal.
- If args dictate GUI args, you Process start the *.exe command in same

dir
passing in the args.

This will allow your console version to use the parent console as you

would
expect (i.e. no new console). And will also allow you to start the GUI
version based on args and leave the current console intact. A desktop

icon
or start menu would point the exe version if you wanted gui. From

console
you could also specify *.exe if you wanted direct access to the gui

version.
hth


Nov 17 '05 #17
Sorry thats: "the exec crashes if you call
system.console.writeline() after FreeConsole()"
MumboJumbo wrote:
I found this code in german online ... it prevents one problem with that
last code posting: "the exec crashes if you call
system.console.writeline()" ...

Jim

[DllImport("Kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);

/// <summary>->Set to true, if the Application should run again
/// even after closing. This is used for setting a new Language.</summary>
public static bool RunAgain=false;

private const Int32 SW_HIDE = 0;

/// <summary>The main entry point for the application.</summary>
[STAThread]
static void Main()
{
try {IntPtr hwnd = GetConsoleWindow();
if (hwnd!=IntPtr.Zero) ShowWindow(hwnd, SW_HIDE);}
catch {}
AnzArgs= Environment.GetCommandLineArgs().Length;
Args = Environment.GetCommandLineArgs();
if (AnzArgs != 1) // Console
{
Console.WriteLine(Application.ProductName+ " started.");
Application.Run(new Form1());
Console.WriteLine(Application.ProductName+ " finished."));
return;
}
do {Application.Run(new Form1());} while (RunAgain);
// Letzteres ist für dynamische Sprach-Umschaltung.

}

MumboJumbo wrote:
Thats perfect!!!!!!!!
Thank you soooo much ...
Jim

Manfred Braun wrote:
Hi All,

I really thought, there would be some help by the experts I am not of
this
type], but I have stored some - for me importent - resoures.

Try the code [C#] at the end, it works for me.

Hope, this helps,
Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

================================================== =================

/*
Compile: csc /t:exe /out:DualTest2.exe DualTest2.cs
from http://www.dotnet247.com/247referenc.../17/88693.aspx
Other console-realted code was published by Matthias Sjögren.
*/

using System;
using System.IO;
using System.Diagnostics;
using System.Configuration;
using System.Runtime.InteropServices; //Needed for WIN32API-calls.
using System.Threading;
using System.Windows.Forms;
class DualTest
{
private static string strAppName = "DualTest";
public static bool useWindow = false;
public static Form Form1 = new Form();
public static int intFreeConsoleRTC;

static int Main(string[] args)
{
//Read application specific settings:
try
{
useWindow =
bool.Parse(ConfigurationSettings.AppSettings["AlwaysUseWindow"]);
}
catch(System.Exception)
{}

if( (args.Length == 0) || (useWindow) )
{
Console.WriteLine("No args, using a window...");
intFreeConsoleRTC = FreeConsole();
if(intFreeConsoleRTC != 0)
{
try
{
/*
The Thread.Sleep() demonstrates, that the console is detached.
If one
starts this proggi with a doubleclick in windows explorer, first,
a console window opens. After calling "FreeConsole()", the
console window disappears. The delay is made, to show this effect,
because opening the form is delayed.
*/
Thread.Sleep(2500);

//First method, de-commented now:
Application.Run(Form1);

//This could also be used!
//System.Windows.Forms.MessageBox.Show("Hello, world!");
}
catch(Exception e)
{
//Do something here
}
}
else
{
//The console was NOT detached for some reasons, we can continue
writing
there...
Console.WriteLine("Console was NOT freed, reason:" +
intFreeConsoleRTC );
}
return 1;
}
else
{
//We use the existing arguments and proceed...
Console.WriteLine(strAppName + ", have all, NOT using a window.");

return 0;
}
}
[DllImport("kernel32.dll")]
private static extern int FreeConsole(); //BOOL FreeConsole(VOID);
}

================================================== =================
"MumboJumbo" <ju**@junk.be> wrote in message
news:1R*********************@news20.bellglobal.com ...

Thank you ... this is what i was trying to get at ...

So you must have two apps if you would like "typical" console
interaction ...

Thanks,
Jim

William Stacey [MVP] wrote:
> A common pattern to achieve this is:
> - Create two apps. One gui and one console by the same name and put

them in

> the same directory.
> - Name the console version *.com and the gui *.exe. The .com will be

found

> first in the normal search order so you console app will start first.
> - Your console app will parse the command line. If console args, the

app

> continues as your normal.
> - If args dictate GUI args, you Process start the *.exe command in
> same

dir

> passing in the args.
>
> This will allow your console version to use the parent console as you

would

> expect (i.e. no new console). And will also allow you to start the
> GUI
> version based on args and leave the current console intact. A
> desktop

icon

> or start menu would point the exe version if you wanted gui. From

console

> you could also specify *.exe if you wanted direct access to the gui

version.

> hth
>

Nov 17 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken VdB | last post by:
Hi everyone, One of the things that really interested me about VS.net was that I can now create a console based app using VB. I have a number of QuickBASIC 4.5 apps which we are still using and...
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...
6
by: MeowCow | last post by:
I will try and make my question with out being too long winded. I have been doing a lot of reading on how to do multithreading and I have implemented the code from the following example on...
7
by: Robert | last post by:
Hello, I'm using Pythonwin and py2.3 (py2.4). I did not come clear with this: I want to use win32-fuctions like win32ui.MessageBox, listctrl.InsertItem ..... to get unicode strings on the...
5
by: Jon Davis | last post by:
Is there such a thing as a Windows standard for killing a console app that is monitored by a System.Diagnostics.Process object? I'm hosting several C# console apps in a C# Windows service and...
0
by: serpius | last post by:
Hello Everyone, I am a beginner in VB.Net 2003 and am taking classes in beginning VB.net. To make a long story short, I am looking for real samples of coding in Console Apps. I am the type of...
4
by: Paul Hemans | last post by:
Hi newbie here. I am learning c++/cli I by writing console apps. When my apps finished they used to display a message at the bottom of the screen, something like "press any key to continue" so that...
3
by: Alex | last post by:
Hello, I'm wroting a console utility in VB 2005, and I need the ability to pass parameters to the application when the program runs. For example, if my program is called testing.exe, I need to...
5
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I'm learning C# via console apps (to avoid getting bogged down in Windows stuff). I'm wondering how the OO paradigm would work. Say I'm developing 'classic' menu-driven apps where a menu gives a...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.