1

I have a winforms application. I would like to run the .exe from the commandline and redirect the output from one of the output textboxes to the commandline. I don't want to launch the winform application just run the logic in the background.

I tried the advice from this thread

C# application both GUI and commandline

but I didn't see any of my console.writeline messages in the command line in cmd.exe when I ran the application via the cmd.exe. Can anyone guide me as to what I might be doing wrong?

I have an if statement that did this logic:

  if(args.Length >0)
   {
     Console.writeline("this has arguments");
     new Mainform();
   }
  else
   {
    Application.EnableVisualStyles();
    Application.Run(new MainForm());
   }

The else part still works. But nothing happens in the if part either using the cmd.exe to run the application or when I use the properties ->debug->command line arguments and give it arguments to run while debugging it. I have no idea what I am doing wrong.

Community
  • 1
  • 1
  • 1
    Move logic into separate dll and use that dll both from forms and from console application – Sergey Berezovskiy Jun 13 '13 at 16:18
  • so make two different applications? One for Winforms and one for Console? – user2482989 Jun 13 '13 at 17:10
  • Exactly. And both applications will use same business logic, which will be independent from UI you are using – Sergey Berezovskiy Jun 13 '13 at 17:20
  • 2
    It isn't visible because a Winforms app doesn't have a console. Pinvoke AllocConsole (not AttachConsole, output gets intermingled). Your form isn't visible because you forgot Application.Run(). – Hans Passant Jun 13 '13 at 17:34
  • That's what I did finally and it worked. Another question though: In order to get the prompt back i need to put System.Windows.Forms.SendKeys.SendWait("{ENTER}"); Does anyone know why? – user2482989 Jun 25 '13 at 14:52

1 Answers1

0

I think you created your Windows Forms project and pasted the code? If so, it couldn't work. You have to create your Console project and here is the code I've tested, works like a charm except that when the Form is shown (GUI mode) the Console is still there, closing that Console will also close your Form:

class Program
{
    static void Main(string[] args)
    {            
            if (args.Length == 0)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else //Process command lines
            {
                Console.WriteLine("Please enter some command.");
                string cmd = Console.ReadLine();
                Console.WriteLine("You entered: " + cmd);
            }            
    }
}

Because the Console project doesn't add reference to System.Windows.Forms.dll automatically for you, so you have to do that manually to be able to create your form in that Console project. The point is that you applied the code in a Winforms project while it should be in a Console project. There is only a way I know which can show a Console window in a winforms application is running the cmd.exe but that will show a new Console window than the existing one which you are using to run your application.

King King
  • 61,710
  • 16
  • 105
  • 130
  • Thanks. yeah I figured that out eventually by myself. I do attach to process now instead and it works. I am trying to find a non hackish way of getting back the prompt without having to hit enter. I used System.Windows.Forms.SendKeys.SendWait("{ENTER}"); but my boss is unhappy with that solution and sees it as hacking it. – user2482989 Jun 25 '13 at 14:36