Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# application both GUI and commandline

I currently have an application with a GUI.

Would it be possible to use this same application from the commandline (without GUI and with using parameters).

Or do I have to create a separate .exe (and application) for the commandline tool?

like image 452
PeeHaa Avatar asked Aug 26 '11 00:08

PeeHaa


People also ask

What do you mean by C?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


2 Answers

  1. Edit your project properties to make your app a "Windows Application" (not "Console Application"). You can still accept command line parameters this way. If you don't do this, then a console window will pop up when you double-click on the app's icon.
  2. Make sure your Main function accepts command line parameters.
  3. Don't show the window if you get any command line parameters.

Here's a short example:

[STAThread] static void Main(string[] args) {     if(args.Length == 0)     {         Application.Run(new MyMainForm());     }     else     {         // Do command line/silent logic here...     } } 

If your app isn't already structured to cleanly do silent processing (if all your logic is jammed into your WinForm code), you can hack silent processing in ala CharithJ's answer.

EDIT by OP Sorry to hijack your answer Merlyn. Just want all the info here for others.

To be able to write to console in a WinForms app just do the following:

static class Program {     // defines for commandline output     [DllImport("kernel32.dll")]     static extern bool AttachConsole(int dwProcessId);     private const int ATTACH_PARENT_PROCESS = -1;      /// <summary>     /// The main entry point for the application.     /// </summary>     [STAThread]     static void Main(string[] args)     {         // redirect console output to parent process;         // must be before any calls to Console.WriteLine()         AttachConsole(ATTACH_PARENT_PROCESS);          if (args.Length > 0)         {             Console.WriteLine("Yay! I have just created a commandline tool.");             // sending the enter key is not really needed, but otherwise the user thinks the app is still running by looking at the commandline. The enter key takes care of displaying the prompt again.             System.Windows.Forms.SendKeys.SendWait("{ENTER}");             Application.Exit();         }         else         {             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault(false);             Application.Run(new QrCodeSampleApp());         }     } } 
like image 87
Merlyn Morgan-Graham Avatar answered Sep 26 '22 20:09

Merlyn Morgan-Graham


In your program.cs class keep the Main method as it is but add string[] Args to the main form. For example...

    [STAThread]     static void Main(string[] Args)     {         ....         Application.Run(new mainform(Args));     } 

In mainform.cs constructor

    public mainform(string[] Args)     {         InitializeComponent();          if (Args.Length > 0)          {              // Do what you want to do as command line application.              // You can hide the form and do processing silently.              // Remember to close the form after processing.          }     } 
like image 26
CharithJ Avatar answered Sep 26 '22 20:09

CharithJ