📜  C#|如何更改控制台的输入编码方案

📅  最后修改于: 2021-05-29 15:59:09             🧑  作者: Mango

给定普通的C#控制台,任务是更改控制台的输入编码方案。

方法:可以使用C#中System包的Console类中的InputEncoding属性来完成此操作。 Console.InputEncoding属性获取或设置控制台用来读取输入的编码。

程序1:获取输入编码方案的价值

// C# program to illustrate the
// Console.InputEncoding Property
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace GFG {
  
class Program {
  
    static void Main(string[] args)
    {
  
        // Get the Input Encoding Scheme
        Console.WriteLine("Current Input Encoding Scheme: {0}",
                                       Console.InputEncoding);
    }
}
}

输出:

程序2:设置输入编码方案的值

// C# program to illustrate the
// Console.InputEncoding Property
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace GFG {
  
class Program {
  
    static void Main(string[] args)
    {
  
        // Get the Input Encoding Scheme
        Console.WriteLine("Current Input Encoding Scheme: {0}",
                                       Console.InputEncoding);
  
        // Set the Input Encoding Scheme to ASCII
        Console.InputEncoding = Encoding.ASCII;
  
        // Get the Input Encoding Scheme
        Console.WriteLine("Current Input Encoding Scheme: {0}",
                                        Console.InputEncoding);
    }
}
}

输出: