The “correct” way to create a .NET Core console app without background services

Instead of a hosted service, I would recommend the following; using (var host = CreateHostBuilder(args).Build()) { await host.StartAsync(); var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>(); // do work here / get your work service … lifetime.StopApplication(); await host.WaitForShutdownAsync(); }

Custom text color in C# console application?

The list found at http://msdn.microsoft.com/en-us/library/system.console.backgroundcolor.aspx I believe are the only supported colors in console. No hex allowed. Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow Gray DarkGray Blue Green Cyan Red Magenta Yellow White EDIT Get the working project files off my public Repo https://bitbucket.org/benskolnick/color-console/ But on further investigation you can do a lot of work … Read more

.Net 6 Console app: WebApplication.CreateBuilder vs Host.CreateDefaultBuilder

WebApplication.CreateBuilderpart() is only used for web/api applications like the name implies Host.CreateDefaultBuilder() is used to build a generic host (without web services, middleware etc) which you can use to build anything other than webhost. See for example; https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host Which has not changed. Its true that it feels a bit awkward to build console apps and/or … Read more

Can I get copy/paste functionality from a C# Console Window?

I’ve copied text from the Console window and pasted it into another source many times. It’s there as default in a Console application; Right click the console border: Select Edit > Mark: Drag over the text you want using the mouse (Or use the arrow keys) to select the text you want: Again, right click … Read more

Run console application from other console app

You can use Process.Start to start the other console application. You will need to construct the process with ProcessStartInfo.RedirectOutput set to true and UseShellExecute set to false in order to be able to utilize the output yourself. You can then read the output using StandardOutput.ReadToEnd on the process.

Is Thread.Sleep(Timeout.Infinite); more efficient than while(true){}?

I would recommend using a ManualResetEvent (or other WaitHandle), and calling ManualResetEvent.WaitOne. This will have a similar effect to sleeping forever, except that it provides you a clean way to exit from your infinite “block” when desired (by calling Set() on the event). Using while(true) will consume CPU cycles, so it’s definitely something to avoid. … Read more

Show message Box in .net console application

We can show a message box in a console application. But first include this reference in your vb.net or c# console application System.Windows.Forms; Reference: To add reference in vb.net program right click (in solution explorer) on your project name-> then add reference-> then .Net-> then select System.Windows.Forms. To add reference in c# program right click … Read more