从控制台读取unicode

时间:2012-02-29 16:04:17

标签: c# .net encoding console

我正在尝试从C#中的控制台读取unicode字符串,例如,让我们使用他的那个:

C:\ SVN \D³ebugger\ SRC \виталик\ Program.cs的

起初我只是尝试Console.ReadLine()给我c:\SVN\D3ebugger\src\???????\Program.cs

我尝试将Console.InputEncoding设置为UTF8,就像Console.InputEncoding = Encoding.UTF8一样,但是返回了我c:\SVN\D³ebugger\src\???????\Program.cs,基本上将字符串的西里尔字母部分弄清了。

所以随机磕磕绊绊我尝试设置这样的编码,Console.InputEncoding = Encoding.GetEncoding(1251);返回c:\SVN\D?ebugger\src\виталик\Program.cs,这次破坏了³字符。

此时似乎通过为InputStream切换encodings,我一次只能获得一种语言。

我也尝试过去做这样的事情:

// 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);

这对于非unicode字符串工作正常,但是,当我试图让它读取我的示例字符串时,应用程序崩溃了。我试图告诉Visual Studio打破所有异常(包括本机异常),然而,应用程序仍会崩溃。

我还在Microsoft的Connect中发现了this open bug,似乎说现在不可能从控制台的InputStream中读取Unicode。

值得注意的是,即使与我的问题没有严格关联,如果Console.OutputEncoding设置为UTF8,Console.WriteLine也可以正常打印此字符串。

谢谢!

更新1

我正在寻找.NET 3.5的解决方案

更新2

使用我使用过的完整原生代码进行了更新。

2 个答案:

答案 0 :(得分:11)

这在目标.NET 4客户端配置文件时似乎工作正常,但遗憾的是在目标.NET 3.5客户端配置文件时没有。确保将控制台字体更改为Lucida控制台 正如@jcl所指出的,即使我已经针对.NET4,这只是因为我安装了.NET 4.5。

class Program
{
    private static void Main(string[] args)
    {
        Console.InputEncoding = Encoding.Unicode;
        Console.OutputEncoding = Encoding.Unicode;

        while (true)
        {
            string s = Console.ReadLine();

            if (!string.IsNullOrEmpty(s))
            {
                Debug.WriteLine(s);

                Console.WriteLine(s);
            }
        }
    }
}

enter image description here

答案 1 :(得分:6)

这是.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

相关问题