« Application console » : différence entre les versions
Apparence
Ligne 96 : | Ligne 96 : | ||
<kode lang='bash'> | <kode lang='bash'> | ||
dotnet add package Microsoft.Extensions.DependencyInjection | dotnet add package Microsoft.Extensions.DependencyInjection | ||
</kode> | |||
<filebox fn='Program.cs'> | |||
static void Main(string[] args) | |||
{ | |||
var services = new ServiceCollection(); | |||
ConfigureServices(services); | |||
} | |||
private static void ConfigureServices(IServiceCollection services) | |||
{ | |||
services.AddTransient<IMyClass, MyClass>(); | |||
} | |||
</filebox> | |||
== [https://www.blinkingcaret.com/2018/02/14/net-core-console-logging/ Logging in .Net Core Console Apps] == | |||
<kode lang='bash'> | |||
dotnet add package Microsoft.Extensions.Logging | dotnet add package Microsoft.Extensions.Logging | ||
dotnet add package Microsoft.Extensions.Logging.Console | dotnet add package Microsoft.Extensions.Logging.Console | ||
Ligne 116 : | Ligne 132 : | ||
private static void ConfigureServices(IServiceCollection services) | private static void ConfigureServices(IServiceCollection services) | ||
{ | { | ||
services.AddLogging(configure => configure.AddConsole() | services.AddLogging(configure => configure.AddConsole()); | ||
} | } | ||
</filebox> | </filebox> | ||
Version du 24 mai 2020 à 20:37
Liens
Code
using System;
namespace MonNS
{
class MaClasse
{
public static void Main(string[] args)
{
Console.WriteLine("");
}
}
}
|
Read input
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Escape) { }
|
Masquer la saisie d'un mot de passe
|
Masquer la fenêtre de la Console
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
|
Couleurs
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("...");
Console.ResetColor();
|
Dependency injection
dotnet add package Microsoft.Extensions.DependencyInjection |
Program.cs |
static void Main(string[] args)
{
var services = new ServiceCollection();
ConfigureServices(services);
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IMyClass, MyClass>();
}
|
Logging in .Net Core Console Apps
dotnet add package Microsoft.Extensions.Logging dotnet add package Microsoft.Extensions.Logging.Console |
Program.cs |
static void Main(string[] args)
{
var services = new ServiceCollection();
ConfigureServices(services);
// log
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetService<ILoggerFactory>()
.CreateLogger<Program>();
logger.LogDebug("Starting application");
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddLogging(configure => configure.AddConsole());
}
|