How to get CMD/console encoding in C#

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

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.


You need Encoding.CodePage property:

var codePage = Console.OutputEncoding.CodePage;

which will give you a code page value (866 in your example).

Tags:

C#

Cmd

Console