Search code examples
.netconsole-applicationright-to-left

Can .NET's System.Console be configured to print Right To Left?


After overcoming the issues in printing non-ascii characters described here, I wonder if is it possible to configure the console to print Right-To-Left ?

I am now playing with Visual Studio T4 templating, wanted to see for my self the outcome of trying to automatically generate Enums out of database look up tables - and for that I wanted to test myself in this console application I am writing for this purpose.

Obviously, as a workaround, printing the following helps me:

new string(str.ToCharArray().Reverse().ToArray())

But does the console support RTL printing ?


Solution

  • If you also want to print your strings aligned to the right side of the screen in addition to have it rendered in a reversed manner (RTL), use this:

    private static void WriteLineRtl(string input)
    {              
      Console.CursorLeft = Console.WindowWidth - input.Length;
      for (int i = input.Length - 1; i >= 0; i--)        
          Console.Write(input[i]); 
    }
    

    Example:

    using static System.Console;
    
    class Program
    {
      static void Main(string[] args)
      {
        WriteLineRtl("שלום,");
        WriteLineRtl("בהמשך לשיחתנו הטלפונית,");
        WriteLineRtl("רציתי להודיע שהכל בסדר.");
        WriteLineRtl("שלום ולהתראות!");
    
        Read();
      }
    
      static void WriteLineRtl(string input)
      {
        CursorLeft = WindowWidth - input.Length;
        for (int i = input.Length - 1; i >= 0; i--)
          Write(input[i]);
      }
    }
    

    Output:

    enter image description here