The solution to your problem explained in details at this
link[
^].
The code from this blog is here (I have nothing to do with this code, it is simply cut/paste):
using System;
using System.Runtime.InteropServices;
public class Test
{
public static void Main()
{
WriteLineRight("दीपक");
}
internal static bool IsConsoleFontTrueType(IntPtr std) {
CONSOLE_FONT_INFO_EX cfie = new CONSOLE_FONT_INFO_EX();
cfie.cbSize = (uint)Marshal.SizeOf(cfie);
if(GetCurrentConsoleFontEx(std, false, ref cfie)) {
return(((cfie.FontFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE));
}
return false;
}
public static void WriteLineRight(string st)
{
IntPtr stdout = GetStdHandle(STD_OUTPUT_HANDLE);
if(stdout != INVALID_HANDLE_VALUE) {
uint filetype = GetFileType(stdout);
if(! ((filetype == FILE_TYPE_UNKNOWN) && (Marshal.GetLastWin32Error() != ERROR_SUCCESS))) {
bool fConsole;
uint mode;
uint written;
filetype &= ~(FILE_TYPE_REMOTE);
if (filetype == FILE_TYPE_CHAR) {
bool retval = GetConsoleMode(stdout, out mode);
if ((retval == false) && (Marshal.GetLastWin32Error() == ERROR_INVALID_HANDLE)) {
fConsole = false;
} else {
fConsole = true;
}
} else {
fConsole = false;
}
if (fConsole) {
if (IsConsoleFontTrueType(stdout)) {
WriteConsoleW(stdout, st, st.Length, out written, IntPtr.Zero);
} else {
}
} else {
WriteFile(stdout, BOM, 2, out written, IntPtr.Zero);
WriteFile(stdout, st, st.Length * 2, out written, IntPtr.Zero);
}
}
}
}
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
internal static extern bool WriteConsoleW(IntPtr hConsoleOutput, string lpBuffer, int nNumberOfCharsToWrite,
out uint lpNumberOfCharsWritten, IntPtr lpReserved);
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
internal static extern bool WriteFile(IntPtr hFile, string lpBuffer, int nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten, IntPtr lpOverlapped);
[DllImport("kernel32.dll", ExactSpelling=true, SetLastError=true)]
internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll", ExactSpelling=true)]
internal static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFO_EX lpConsoleCurrentFontEx);
[DllImport("Kernel32.DLL", ExactSpelling=true, SetLastError=true)]
internal static extern uint GetFileType(IntPtr hFile);
[DllImport("Kernel32.DLL", ExactSpelling=true)]
internal static extern IntPtr GetStdHandle(int nStdHandle);
internal struct COORD {
internal short X;
internal short Y;
internal COORD(short x, short y) {
X = x;
Y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct CONSOLE_FONT_INFO_EX {
internal uint cbSize;
internal uint nFont;
internal COORD dwFontSize;
internal int FontFamily;
internal int FontWeight;
fixed char FaceName[LF_FACESIZE];
}
internal const int TMPF_TRUETYPE = 0x4;
internal const int LF_FACESIZE = 32;
internal const string BOM = "\uFEFF";
internal const int STD_OUTPUT_HANDLE = -11;
internal const int ERROR_INVALID_HANDLE = 6;
internal const int ERROR_SUCCESS = 0;
internal const uint FILE_TYPE_UNKNOWN = 0x0000;
internal const uint FILE_TYPE_DISK = 0x0001;
internal const uint FILE_TYPE_CHAR = 0x0002;
internal const uint FILE_TYPE_PIPE = 0x0003;
internal const uint FILE_TYPE_REMOTE = 0x8000;
internal static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
}