Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:22:15+00:00 2026-05-31T02:22:15+00:00

I am trying to read unicode string from a console in C#, for the

  • 0

I am trying to read unicode string from a console in C#, for the sake of example, lets uset his one:

c:\SVN\D³ebugger\src\виталик\Program.cs

At first I just tried to Console.ReadLine() which returned me c:\SVN\D3ebugger\src\???????\Program.cs

I’ve tried to set the Console.InputEncoding to UTF8 like so Console.InputEncoding = Encoding.UTF8 but that returned me c:\SVN\D³ebugger\src\???????\Program.cs, basically mucking up the Cyrillic part of the string.

So randomly stumbling I’ve tried to set the encoding like that, Console.InputEncoding = Encoding.GetEncoding(1251); which returned c:\SVN\D?ebugger\src\виталик\Program.cs, this time corrupting the ³ character.

At this point it seems that by switching encodings for the InputStream I can only get a single language at a time.

I’ve also tried going native and doing something like that:

// Code
public static string ReadLine()
{
    const uint nNumberOfCharsToRead = 1024;
    StringBuilder buffer = new StringBuilder();

    uint charsRead = 0;
    bool result = ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), buffer, nNumberOfCharsToRead, out charsRead, (IntPtr)0);

    // Return the input minus the newline character
    if (result && charsRead > 1) return buffer.ToString(0, (int)charsRead - 1);
    return string.Empty;
}

// Extern definitions

    [DllImport("Kernel32.DLL", ExactSpelling = true)]
    internal static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    static extern bool ReadConsoleW(IntPtr hConsoleInput, [Out] StringBuilder lpBuffer, 
        uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead, IntPtr lpReserved);

That was working fine for non-unicode strings, however, when I tried to make it read my sample string, the application crashed. I’ve tried to tell Visual Studio to break on ALL exception (including native ones), yet, the application would still crash.

I also found this open bug in Microsoft’s Connect that seems to say that it is impossible right now to read Unicode from the console’s InputStream.

It is worth noting, even though not strictly related to my question, that Console.WriteLine is able to print this string just fine, if Console.OutputEncoding is set to UTF8.

Thank you!

Update 1

I am looking for a solution for .NET 3.5

Update 2

Updated with the full native code I’ve used.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:22:16+00:00Added an answer on May 31, 2026 at 2:22 am

    Here’s one fully working version in .NET 3.5 Client:

    class Program
    {
      [DllImport("kernel32.dll", SetLastError = true)]
      static extern IntPtr GetStdHandle(int nStdHandle);
    
      [DllImport("kernel32.dll")]
      static extern bool ReadConsoleW(IntPtr hConsoleInput, [Out] byte[]
         lpBuffer, uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead,
         IntPtr lpReserved);
    
      public static IntPtr GetWin32InputHandle()
      {
        const int STD_INPUT_HANDLE = -10;
        IntPtr inHandle = GetStdHandle(STD_INPUT_HANDLE);
        return inHandle;
      }
    
      public static string ReadLine()
      {
        const int bufferSize = 1024;
        var buffer = new byte[bufferSize];
    
        uint charsRead = 0;
    
        ReadConsoleW(GetWin32InputHandle(), buffer, bufferSize, out charsRead, (IntPtr)0);
        // -2 to remove ending \n\r
        int nc = ((int)charsRead - 2) * 2;
        var b = new byte[nc];
        for (var i = 0; i < nc; i++)
          b[i] = buffer[i];
    
        var utf8enc = Encoding.UTF8;
        var unicodeenc = Encoding.Unicode;
        return utf8enc.GetString(Encoding.Convert(unicodeenc, utf8enc, b));
      }
    
      static void Main(string[] args)
      {
        Console.OutputEncoding = Encoding.UTF8;
        Console.Write("Input: ");
        var st = ReadLine();
        Console.WriteLine("Output: {0}", st);
      }
    }
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that manipulates unicode strings read in from a
I am trying to read in some sentences from a file that contains unicode
I am trying to parse some unicode text from an excel2007 cell read by
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
I am trying to read ASCII text response from a tcp open streaming socket
I'm trying to use urllib and urllib2 to read from a text file that
Hey trying to read stream into image control can any one help also I
I am trying to read from Python the WM_COPYDATA message some applications (I'm trying
I'm trying to read a file from the SD card and I've been told
I'm trying to read a simple unicode (UTF-16) text file with just some numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.