Home

Set code page in Util.Cmd

I'm obviously doing something daft here, but is there a straightforward way to set the code page and then run a bat file using Util.Cmd?

void Main()
{
var pathToBat = @C:\Users\Me\some thing.bat;
Util.Cmd("chcp 65001 \"{pathToBat}\"");
}

I'm getting a CommandExecutionException and path is being truncated due to the space in the path, but looking at GetCmdProcess I see it also adds surrounding quotes. Is there something obvious I'm missing?

Thanks John

Comments

  • Shouldn't that be

    Util.Cmd($"chcp 65001 & \"{pathToBat}\"");

  • Hi,

    Arg! I'm pretty sure that's what I tried first (and didn't type correctly as the question (sorry)). So trying again you're quite right, the syntax is correct and the file runs, but the output in LINQPad is still unrecognised characters.

    For example, using this in the bat file:

    echo cédille

    and running

    Util.Cmd($"chcp 65001 & \"{pathToBat}\"");

    produces this in the output window

    cédille

    Thiis is the same even if you put chcp 65001 into the bat file itself (which I'd rather avoid anyway).

    Is there a setting somewhere for the results window?

    Thanks,

    John

  • Works in LinqPad 6, so I presume you are using LinqPad 5.
    Using ILspy (F12) it looks like the difference is that 5 does not set StandardOutputEncoding

    So, a very simple example to try would be

    var pathToBat = @C:\Users\Me\some thing.bat;
    
    ProcessStartInfo processStartInfo = new ProcessStartInfo
    {
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        UseShellExecute = false,
        CreateNoWindow = true,
    
        StandardOutputEncoding = Encoding.UTF8,
        StandardErrorEncoding = Encoding.UTF8
    };
    processStartInfo.FileName = pathToBat;
    var p = Process.Start(processStartInfo);
    p.WaitForExit();
    p.StandardOutput.ReadToEnd().Dump();
    

    If it works, you would be better to use ILspy to look at how to do this properly.

  • Oh great. That's good to know. Thank you for looking at this. I am using 5, but I also have 6 on this machine and will compare the two. Will have a go with your code and report back. Many thanks.

  • This works nicely and the output is as expected - thank you.

    I also tried wrapping up the Cmd and GetCmdProcess (adding the encoding to the latter) into a static class in the query and this also worked well too.

    I see that ProcessStartInfo.StandardInputEncoding is not available in .net framework but for my purposes, output in the main focus.

    Anyway, thanks again for your help.

    Best regards

    John

Sign In or Register to comment.