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

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

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

方法:可以使用C#中System包的Console类中的OutputEncoding属性来完成此操作。

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

// C# program to illustrate the
// Console.OutputEncoding 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 Output Encoding Scheme
        Console.WriteLine("Current Output Encoding Scheme: {0}",
                                        Console.OutputEncoding);
    }
}
}

输出:

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

// C# program to illustrate the
// Console.OutputEncoding 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 Output Encoding Scheme
        Console.WriteLine("Current Output Encoding Scheme: {0}",
                                        Console.OutputEncoding);
  
        // Set the Output Encoding Scheme to ASCII
        Console.OutputEncoding = Encoding.ASCII;
  
        // Get the Output Encoding Scheme
        Console.WriteLine("Current Output Encoding Scheme: {0}",
                                        Console.OutputEncoding);
    }
}
}

输出: