0

I have created an C# application, I need to access to my app in 2 modes:

  1. GUI: via mouse or via ms dos console like this: app.exe
  2. CommandLine: via ms dos console like this(without GUI interface): app.exe -c <some_other_options>

The problem when I configured my application like Windows Application The console version is not launched. But when I changed it to Console Application it launched with command line in 2 desired modes but when I launch it via click by mouse I had a black console opened with my gui and when I close the black console, the GUI is also closed.

Is it possible to make my need via c#?

There is my main function:

        [STAThread]
        static void Main(string[] args)
        {           
            var options = new Options();
            var parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));

            if (args.Count() != 0)
            {
                try
                {
                    if (!parser.ParseArguments(args, options))
                        Environment.Exit(1);
                    customfunc(options);
                    Environment.Exit(0);
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("Fatal error on parsing: " + e.Message);
                    Environment.Exit(1);
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmApp());
            }
        }
developer
  • 4,744
  • 7
  • 40
  • 55

1 Answers1

2

One way I could see it is to have 3 assemblies :

  • 1 common class library project (where you'd put all your logic code
  • 1 Windows Forms (or WPF) exe app
  • 1 console application project

Both the winforms and console app would reference the same common library for the background logic, and you only have implement the UI part in each app project. The only problem with that is that you'll have to have 2 different executable for each of the applications.

I do think there might be a way to do what you want in the same app, but I have no idea how to make the console app not show the console window.

Noémie Lord
  • 751
  • 4
  • 22