C#用Process类调用cmd时,命令行那里首字母乱码

//获取环境变量
string processName = Environment.GetEnvironmentVariable("ComSpec");
Process myprocess = new Process();
try
{
myprocess.StartInfo.UseShellExecute = false;
myprocess.StartInfo.FileName = processName;
myprocess.StartInfo.RedirectStandardInput = true;
myprocess.Start();
string commandname = "ping http://www.baidu.com";
myprocess.StandardInput.WriteLine(commandname );
}
catch(Exception e)
{
print(e.Message);
}

dos命令那里首字母是乱码,不管换成什么命令,总是首字母乱码。

图片说明

C#本身字符串没有编码问题,但是从外部获取的就不好说。你可以用encoding.getbytes()看看对应的字节码有没有问题

检查一下引号等,是否字符有问题

string processName = Environment.GetEnvironmentVariable("ComSpec");
错误在这里,你的环境变量重新设置下看看

你是创建的控制台程序?我这创建的WinForm程序调用的窗口出现一下就没了。

在myprocess.Start()之前设置控制台的输入编码:
System.Console.InputEncoding = System.Text.Encoding.UTF8;

亲测可以解决乱码的问题