Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to make win app also available for command line 2

Status
Not open for further replies.

rickinger

Programmer
Feb 11, 2004
20
0
0
NL
Well the subjects say it all. I want to able to use my windows application as a command line tool as well.
What do I need to implement to prevent the application from opening a window and instead read the command line args??
 
Create a class with a static Main() method and set this as your startup object.

Code:
[STAThread]
static void Main()
{
  // do whatever you want here
}

If you decide that you want to display a form include (using whatever form you want to display instead of Form1, of course):
Code:
Application.Run(new Form1());

You then need to go into the properties for your project and set the Startup Object to this new class.
 
In addition you can do the folowings if there are command line parameters.:
Code:
static void Main(string[] args)
{

   MyClassProcessArguments(args);
   Application.Run(new Form1(args[0], args[1]));
   //...
	
}

public void MyClassProcessArguments(string [] args)
{
   foreach(string arg in args)
	{
	}
//...
}
-obislavu-
 
Thanks guys... After setting the startup object and the application type to console the console responds.
 
woaw, no wait...
Now I get the console window even when I start the application through windows... Any way to get rid of that console window when the application was not started over the console?
 
Hm, anybody an idea how to get rid of that console window when the application was not started over the console?
 
Call Application.Exit() or Application.ExitThread() in order to stop all running message loops on all threads and closes all windows of the application.
Also you should call the Form.Close() method for each open form individually before calling the Exit method.
-obislavu-


 
Hi obislavu,

thanks, but I don't have any running message loops, and the console window will not close this way...:

static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new Form1());
Application.Exit();
}
[...]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top