This is not a duplicate! - Well, after reading the comments, maybe it is.
I was looking for a way to italicize text in the console output of a console application, in c#, Visual Studio 2015, Targeting .NET Framework 4.5.2, OS = Windows 7.
The Microsoft Documentation is pretty clear
It's here - and it's so misleading it's wrong. This is an OS problem.
I found the following question with a solution that does what I want by Vladimir Reshetnikov,
adding text decorations to console output
answered Mar 28 at 19:52 in one of the answers, and code like it in git, and elsewhere... my problem is - naturally - it doesn't work for me.
I copied the author's code with minor mods into the following console application
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
const int STD_OUTPUT_HANDLE = -11;
const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
static void Main()
{
var handle = GetStdHandle(STD_OUTPUT_HANDLE);
uint mode;
GetConsoleMode(handle, out mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(handle, mode);
const string UNDERLINE = "\x1B[4m";
const string RESET = "\x1B[0m";
Console.WriteLine("Some " + UNDERLINE + "underlined" + RESET + " text");
Console.ReadLine();
}
}
}
and I get the VT commands in the window, instead of underline, as in the article.
Here's my console window:
I've trapped the return value from ConsoleSetMode - it's zero. I've seen this failure with lasterror = 6, but the lasterror here is 0.
Think it's a recent update? ... or something? [edit] It's a Windows version problem - Windows 10 AU, apparently, is required.