The standard solution to this is to have two executables. This is exactly
what Visual Studio .NET does.
The technique relies on two facts:
(1) the command line always prefers .COM executables to .EXEs
(2) a .COM executable is allowed to be a proper PE executable (it doesn't
have to be an old-style DOS .COM app)
So you make the .COM the console application, and the .EXE the Windows
application. In the console application, if no parameters are passed on the
command line, just launch the EXE and then exit.
This is why when you just type "devenv" at the command prompt VS.NET
launches in the usual way (assuming VS.NET is on your path), but if you pass
in parameters, it runs as a console app.
Of course you probably don't want to ship two copies of your application for
this purpose...
In practice what you usually want to do is have one real copy of the
application. This is workable - I have a little shim launcher program whose
sole purpose is to allow what you're trying to do here - to allow a windows
app to run normally when run without parameters, but to run as a console app
when parameters are passed. You can use the same shim application for
everything - all you need to do is compile it and then rename. The source
code is here:
http://www.interact-sw.co.uk/utiliti...onsole/source/
Compile this into a file called "MyApplication.com". (Or whatever you want
to call it - so long as it ends in .com.) Then copy the real EXE into the
same directory. (You must build the EXE as a Windows Application, not a
Console application.)
The nice thing about this launcher program is that you don't really need to
do anything special in the main exe. You will be able to access the command
line switches in the normal way from either Environment.CommandLine, or via
the 'args' parameters passed to main. You don't really need to anything
special at all. If you run it as
MyApplication
from the command line, it'll just run as a normal windows app, without
attaching to the console. If you run it as:
MyApplication /some /parameters
then it will run as a console application.
And as for running the program from explorer, just double click on the EXE.
Hope that helps.
--
Ian Griffiths -
http://www.interact-sw.co.uk/iangblog/
DevelopMentor -
http://www.develop.com/
"Mark Allison" wrote:
OK, I found out where - I did this to get it to work. In Visual Studio,
click Project, Properties; in the Common Properties section, General,
Output Type is Console Application.
I now have another issue. If I launch the application from Windows
Explorer, a blank console window gets launched with it. How do I prevent
that?
It works fine if launched from a console. Thanks for your help.
Mark Allison wrote: Thanks Morten, here's my Main method:
[STAThread]
public static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new frmFindDBWin());
}
else
{
Console.WriteLine ("Nothing happens here");
}
}
How do I compile it as a Console Application?
Morten Wennevik wrote:
Hi Mark,
Could you show us your Main method?
Are you compiling as Windows or Console application? I believe you need
to build it as a Console Application to make it work