3

Before anyone marks this as Duplicate, Please read

I have a process which is Windows Application and is written in C#.

Image showing project Properties

Now I have a requirement where I want to run this from console as well. Like this.

enter image description here

Since I need to show output to console, so I changed the application type to Console Application

enter image description here

Now the problem is that, whenever user launches a process from Windows Explorer (by doubling clicking). Console window also opens in behind.

enter image description here

Is there is way to avoid this ?

What I tried after @PatrickHofman's help.

But I still have problems

Community
  • 1
  • 1
SimpleGuy
  • 2,764
  • 5
  • 28
  • 45
  • What about hiding the form when parameters are passed? – C4d Aug 11 '15 at 11:05
  • 5
    *Before anyone marks this as Duplicate, Please read*. Before asking questions, first search... – Patrick Hofman Aug 11 '15 at 11:06
  • @PatrickHofman This is not at all a Duplicate of that one. There the context is to Enable Visual Styles in Application when it is console. here it is to Disable Visual Styles when app is Windows Forms but is run from CLI. And there is no api to disable visual styles. So, you must read before to proceed to mark as Duplicate. it was written for people "like you" only. – SimpleGuy Aug 11 '15 at 11:12
  • 1
    How is this not a duplicate? You want to hide the console? Then fix it as described: http://stackoverflow.com/a/279811/993547. Works like a charm. – Patrick Hofman Aug 11 '15 at 11:13
  • @PatrickHofman No. It isn't working. The prompt is not stopping on console. It returns to the new prompt when app starts. In a real Console Application. the prompts waits until the process completes. And that is what I want.. Ig u hv an idea, do tell me.. or you can even that code, it is not working. – SimpleGuy Aug 11 '15 at 11:19
  • 1
    It works fine here. Did you follow the steps as in the answer? – Patrick Hofman Aug 11 '15 at 11:21
  • And what did you mean with "In a real Console Application. the prompts waits until the process completes.". It does, right? – Patrick Hofman Aug 11 '15 at 11:21
  • @PatrickHofman yes I followed. Only thing which isn't mentioned there, so I have assumed. I have configured my project as "Windows Application". So using that peice of code, my prompt isn't waiting for process to compkete, Niether anything is printed. – SimpleGuy Aug 11 '15 at 11:42
  • @PatrickHofman yes, In real ConsoleApplication prompt waits for process to complete. In my case, using that recommended piece of code, it isn't waiting.. and nothing is printed on Console – SimpleGuy Aug 11 '15 at 11:43
  • @PatrickHofman I did some changes, after that it is working, but a new console is opened, I don't want that – SimpleGuy Aug 11 '15 at 11:44
  • From the linked question, second answer: The correct way to call AllocConsole is to pass -1 to it. This causes our process to attach to the console of our parent process (the console window that launched us). – Patrick Hofman Aug 11 '15 at 11:57
  • @PatrickHofman I did that too, but it has other problems . See last lines of that answer "....After many hours of hunting and experimenting, I've come to the conclusion that there is no way to do this perfectly" – SimpleGuy Aug 11 '15 at 12:00
  • You can call "FreeConsole" but, it does show the console screen momentarily.. Which also isnt quite what you need – BugFinder Aug 11 '15 at 12:02
  • I will reopen your question. Please update your question with your current code, its behavior and what is wrong with it. – Patrick Hofman Aug 11 '15 at 12:10
  • @PatrickHofman Thanks – SimpleGuy Aug 11 '15 at 12:15
  • 1
    Possible duplicate of [Show/Hide the console window of a C# console application](http://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application). Yes I read it. It was asked. Nothing new in the question. – Sinatr Aug 11 '15 at 12:17
  • @Sinatr No it isn't. There post says that App is `ConsoleApp`. Mine is `Windows App`. – SimpleGuy Aug 11 '15 at 12:22
  • 1
    There's basically no way to do what you want to do cleanly. You have to live with some downsides, which depend on whether you run as a console or run as a GUI application. Many people have torn their hair out at this problem (I'm one of them). Note that Visual Studio has devenv.com and devenv.exe - that's because of this issue! Hans Passant has some suggestions here http://stackoverflow.com/questions/15952892/using-the-console-in-a-gui-app-in-windows-only-if-its-run-from-a-console one of which is the VS trick. Maybe that helps. – Sam Aug 11 '15 at 12:42

1 Answers1

4

OK I thought I'd have a play at this as I was curious.

  • First I updated the Main method in Program.cs to take arguments so I could specify -cli and get the application to run on the command line.
  • Second I changed the project's output type to "console application" in the project properties.
  • Third I added the following methods to Program.cs

    private static void HideConsoleWindow()
    {
        var handle = GetConsoleWindow();
    
        ShowWindow(handle, 0);
    }
    
    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
  • Fourth I call HideConsoleWindow as the first action in non-CLI mode.

After these steps my (basic) application looks like:

    [STAThread]
    static void Main(string[] args)
    {
        if (args.Any() && args[0] == "-cli")
        {
            Console.WriteLine("Console app");
        }
        else
        {
            HideConsoleWindow();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

Then if I open the program on the command line with the -cli switch it runs as a command line application and prints content out to the command line and if I run it normally it loads a command line window (extremely) briefly and then loads as a normal application as you'd expect.

Sam
  • 3,320
  • 1
  • 17
  • 22
meh-uk
  • 2,031
  • 1
  • 23
  • 36
  • Thanks ! I was looking for genius like you. the answer really helped me, especially the trick of `/t:exe or /t:winexe`. – SimpleGuy Aug 12 '15 at 03:51
  • Sorry but this answer makes no sense! This is exactly the same as other SO posts that suggest making a console app and immediately hiding the window (which is also the same thing that devenv.com does). Removing `` from the csproj is the same as setting the project to a console project. Adding `/t:exe or /t:winexe` to the "conditional compilation symbols" does absolutely nothing. They are NOT conditional compilation settings, they are arguments for the C# compiler. In fact if you look at your build log you'll see that they are ignored. – Sam Aug 12 '15 at 11:20
  • The link in the answer does NOT say to put those flags in the conditional compilation settings. For reference, here are the C# compiler command-line arguments: https://msdn.microsoft.com/en-us/library/6ds95cz0.aspx `/t` is an alias for `/target`. This answer simple sets your project to a console application, and hides the console if not started with the command-line argument. – Sam Aug 12 '15 at 11:22
  • To be perfectly honest it probably is the same answer as the others, except it came across to me as if this approach was the trade-off SimpleGuy was looking for, and that to be honest those answers aren't actually particularly clear on what you have to do. – meh-uk Aug 12 '15 at 14:15
  • I do agree that the step about the targets was redundant - so I've edited the answer accordingly. – meh-uk Aug 12 '15 at 14:20
  • 1
    Thanks, that's better. I also made one further edit to clarify that it's not necessary to edit the csproj (because you can just set the "console" setting in the properties). I agree, this answer consolidates all the steps from a couple of other answers. – Sam Aug 13 '15 at 01:03