Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET app running as either Windows Form or as Console Application

I am looking to have one of my Windows Forms applications be run programmatically—from the command line. In preparation, I have separated the logic in its own class from the Form. Now I am stuck trying to get the application to switch back and forth based on the presence of command line arguments.

Here is the code for the main class:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        string[] args = Environment.GetCommandLineArgs();
        if (args.Length > 1) // gets passed its path, by default
        {
            CommandLineWork(args);
            return;
        }         

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    private static void CommandLineWork(string[] args)
    {
        Console.WriteLine("It works!");
        Console.ReadLine();
    }

where Form1 is my form and the It works! string is just a placeholder for the actual logic.

Right now, when running this from within Visual Studio (with command line arguments), the phrase It works! is printed to the Output. However, when running the /bin/Debug/Program.exe file (or /Release for that matter) the application crashes.

Am I going about this the right way? Would it make more sense (i.e. take less developer time) to have my logic class be a DLL that gets loaded by two separate applications? Or is there something entirely different that I'm not aware of?

Thanks in advance!

like image 633
mkenyon Avatar asked May 11 '10 19:05

mkenyon


People also ask

What is the difference between Windows Forms application and console application?

A Windows form application is an application that has a graphical user interface(GUI) like the Visual C# IDE. A console program on the other hand is a text application. There are not fancy controls like buttons or textboxes in a console application and they are run from the command prompt.

What is the difference between Windows Forms app and Windows Forms app .NET framework?

Windows Forms App(. NET) is the type which makes desktop apps also but it uses . NET Core (Latest Version is . NET Core 5.0) The Windows Control Library project template is used to create custom controls to use on Windows Forms like we use button from the tool box Developers use the .

What is .NET console application?

Console applications are designed to be used from a "text-only" interface, or run without any interface by another automated tool.

How do I run a .NET core console app on Windows?

Right click the Console App Project and select Publish. Then change Deployment Mode to Self-contained or Framework dependent. . NET Core 3.0 introduces a Single file deployment which is a single executable. Use "framework dependent" if you know the target machine has a .


1 Answers

You'll need to P/Invoke AllocConsole() if you detect a command line argument. Check my answer in this thread for the required code. A C# sample is further down the page. Repeated here because I don't trust that crummy forum site:

using System;
using System.Windows.Forms;

namespace WindowsApplication1 {
  static class Program {
    [STAThread]
    static void Main(string[] args) {
      if (args.Length > 0) {
        // Command line given, display console
        AllocConsole();
        ConsoleMain(args);
      }
      else {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
      }
    }
    private static void ConsoleMain(string[] args) {
      Console.WriteLine("Command line = {0}", Environment.CommandLine);
      for (int ix = 0; ix < args.Length; ++ix)
        Console.WriteLine("Argument{0} = {1}", ix + 1, args[ix]);
      Console.ReadLine();
    }

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern bool AllocConsole();
  }
}
like image 182
Hans Passant Avatar answered Sep 18 '22 14:09

Hans Passant