Hide the console (completely) for a Console app, but only sometimes - c#

I have a C# Console app than runs a pre-build step (to get NuGet Packages).
When I am debugging this, I want to pass in a parameter and show the console. When I am not debugging it I don't want to see it. I don't even want it to flash up there for a second.
I have found ways to hide it, after it has shown. But I can't find a way to never make it show unless I am willing to change it from a console app to a Windows app. (Which I would do if I could then find a way to show the Console when needed.)

Build as a Windows application and show the console when you need it. To show the console when needed use P/Invoke to call AllocConsole (pinvoke.net has the declaration you need).
(Console sub-system processes always get a console, their parent process's if there was one, otherwise a new one. This is the way Windows works at a deep level.)

Use FreeConsole WINAPI function:
http://pinvoke.net/default.aspx/kernel32/FreeConsole.html
Another solution is to simply switch to a WinForms application as project type. No console will be allocated then (and you do not need to show a form).

Related

Is it possible to capture the console screen under Windows?

I want to make a GUI Windows application that can run console applications. When this happens, the console window should not be shown. Instead its content should be shown in a visual component (memo/richedit). This component should show exactly the same content which would appear in the console window, even the color of the text and its background should be displayed. So this visual component should work exactly as a console window. I know that the standard output can be captured but many console applications do not use it. Is it possible to capture the output of a console application this way? Are there Windows API calls that can handle this?
I use Delphi XE2 but C# code would also be helpful.
You have to run the console mode program with stdout redirection to a pipe that your Delphi program will create. Your Delphi program can read the pipe to get the console mode program output and do whatever it needs to. By the way, this works not only with Delphi but also with any language able to create pipe and run program with I/O redirection.
If you need Delphi code to do that, have a look at this reference.
There is a ready-to-run component on GitHub: DosCommand
The Demo shows two ways how to do what you describe.
I am not sure if it works for older versions like XE2, but at least you can give it a try.
Traditionally you would call CreateProcess with stdin/stdout set to pipes you created. This should work for most programs but not for anything that uses a ncurses style "GUI" and you also lose the color information. An example can be found on MSDN.
Windows 10 (1809?) added support for pseudoconsoles. This is used by the new Terminal application and is your best bet for full console compatibility.
The last alternative is to inject into the child process and hook WriteFile, ReadFile and all the console functions but this is ugly and error-prone.

Console Application reading path from textbox WinForms (C#)

I have a console application that converts .txt files into .xml files. I made a WinForms application to make a menu and select a Console Application and run it. The user can choose the paths of the input files through a browse button, and it insert the paths as string into some textboxes. Now I want the Console App to read what they say so it can locate the input files.
Is there any way to do this?
It is pretty easy to launch a console app from a WinForms app by simply using a process. Example:
Process.Start("myConsoleApp.exe", "some arguments");
However, the only time this makes sense is when the console app is not your own code. Or if it is your own code, but it is a generic app that is called from different UIs or methods.
If none of the above is the case (which seems to be your case), it makes more sense to merge the UI and functionality in one app. You wanted the menu to be a generic app that calls different apps, so simply remove the UI related to the console app, make it a separate WinForms app, and add the code from the console app to it. Now your menu app will simply call a WinForms app that has the UI and functionality all in one. You will use the same method above, but likely you don't need to pass any arguments:
Process.Start("myWinformsApp.exe");
From your winform application you can launch another process (probably by passing exe path of console app you talking about and relevant i/p parameter of type string which is basically path of your text file provided by user)
This link might help you do that.

Open console window in mono

I'd like my Mono application to be able to open and close a console window at will. I know how to do this on Windows with it's kernel32.dll but I'd like it to be platform-independent. If this is not possible I want it to implement different methods for the different OS's.
If it is not possible at all I can always resort to using a self-made console window but that would probably take way longer to implement and won't be as compatible. I really want to be sure whether it's possible or not before I work on a custom console window.
The console window only has to be used for output, no input functionality is required. (It will only feature as a way to show log messages.)

Run code once and exit formless app: Windows Forms or WPF... does it matter?

I need to write a very small application which writes some system data to a file and then exits. I could do this in a console application but I have no need or desire for a console window to appear during this process.
I would normally use a Windows Forms application with no forms, execute the code in the Main method and then allow the application to exit, however, this time I couldn't help but wonder if this is the best way to do it and whether you could do it with a WPF application instead, what the differences are and whether or not once you've remove any forms/windows and unnecessary reference, it matters or not.
WPF and WinForms are two different libraries that show UIs.
If you never show a UI, you aren't using either of them.
You should start with a WinForms project (WPF projects set extra project metadata that you don't want), then delete the reference to System.Windows.Forms.dll.
Alternatively, start with a console project, then change the Output type to Windows Application.
Windows Forms with no window or console app with the type changes to windows application will give you the same result which is a simple app with Main() method and now windows.
WCF will only make sense if you actually want to display something as you're not going to use any of its features in your case.

How can I run an application and do not show the console?

I've got a console application and I want to run it from another application. But when I invoke it from code--to call program I use ActiveXObject--it shows the console. How do I get it to not show the console? I mean, the program works but console is shown.
If you make the application a Windows Application instead of a Console Application it will not show or open (or use) a Console. Your code can stay the same - and the application will still function (without ever creating a Form or Window).
That being said, if you control the "other" application, you may want to consider making a class library instead of a separate Application. This would allow you to just include and run code within the library without starting a separate process.
You can create it as a Windows Forms application with a hidden window. That's just one possibility like Reed Copsey pointed out, i used it once because i needed to process some specific Window Messages.

Categories

Resources