如何获得在C#中CMD /控制台编码 [英] How to get CMD/console encoding in C#
问题描述
我需要指定正确的代码页打包带拉链的库文件。依我之见,我需要指定(在我的案件866)主机编码
I need to specify the correct codepage to pack the files with zip library. As I see, I need to specify console encoding (866 in my case).
C:\Users\User>mode
Status for device CON:
----------------------
Lines: 300
Columns: 130
Keyboard rate: 31
Keyboard delay: 1
Code page: 866 <- I need to get this value in C# code
Console.OutputEncoding收益1251这不是我所需要的。
Console.OutputEncoding returns 1251, which is not what I need.
谢谢,
亚历
更新1:显然,在CMD.EXE执行模式并解析输出应该工作,但它似乎太失礼。 。我正在寻找.NET解决方案
Update 1: Obviously, execute "mode" in cmd.exe and parse output should work but it seems too rude. I'm looking for .NET solution.
更新2:的应用程序是Windows窗体应用程序,而不是一个控制台应用程序
Update 2: The application is windows forms application, not a console app.
推荐答案
的默认的一个控制台模式的应用程序的代码页是由系统区域设置决定。控制面板+区域和语言,管理选项卡,更改系统区域设置。您的Windows代码页是斯拉夫语,所以您的控制台代码页所以有一个合理的机会,这个代码将工作:
The default code page for a console mode app is determined by the system locale. Control Panel + Region and Language, Administrative tab, Change System Locale. Your Windows code page is Cyrillic, so is your console code page so there's a reasonable chance that this code will work:
int lcid = GetSystemDefaultLCID();
var ci = System.Globalization.CultureInfo.GetCultureInfo(lcid);
var page = ci.TextInfo.OEMCodePage;
// etc..
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern int GetSystemDefaultLCID();
的不要的避免编写这样的代码,8位文本编码是矿区。当然有没有什么像样的理由,运行一个控制台模式的ZIP程序,有很多可用的.NET压缩库。
Do avoid writing code like this, 8-bit text encodings are a mine field. There certainly isn't any decent reason to have to run a console-mode zip program, there are plenty of .NET zip libraries available.
这篇关于如何获得在C#中CMD /控制台编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!