You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改调用PowerShell执行命令返回的输出语言

问题原因

你修改当前线程的CultureInfo仅作用于你当前的WFP应用进程,启动的PowerShell属于独立的子进程,默认会继承系统层面的区域语言设置,不会继承你父进程的线程文化配置,因此修改不生效。

解决方案

方案1:在PowerShell执行命令前强制修改会话文化(推荐)

直接在PowerShell的执行参数中加入文化设置语句,修改当前PowerShell会话的生效文化,再执行目标命令,仅需修改你的Arguments属性即可,同时修正你原有代码的变量名笔误:

private void myfunction()
{
    Process myprocess= new Process();
    myprocess.StartInfo = new ProcessStartInfo()
    {
        UseShellExecute = false,
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "Powershell.exe",
        // 新增PowerShell会话内的文化配置
        Arguments = "-Command \"& { [System.Globalization.CultureInfo]::CurrentUICulture = 'en-US'; [System.Globalization.CultureInfo]::CurrentCulture = 'en-US'; netsh wlan show interface }\"",
        RedirectStandardError = true,
        RedirectStandardOutput = true
    };
    myprocess.Start();
    String mystring= myprocess.StandardOutput.ReadToEnd();
    myprocess.WaitForExit();
}

方案2:给子进程配置语言环境变量

如果方案1不生效,可以在启动进程前给子进程添加语言相关的环境变量,强制子进程使用英文输出:

CultureInfo newCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = newCulture;
Thread.CurrentThread.CurrentCulture = newCulture;

Process myprocess= new Process();
myprocess.StartInfo = new ProcessStartInfo()
{
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden,
    FileName = "Powershell.exe",
    Arguments = "netsh wlan show interface",
    RedirectStandardError = true,
    RedirectStandardOutput = true
};
// 给子进程添加语言环境变量
myprocess.StartInfo.EnvironmentVariables["LANG"] = "en-US";
myprocess.StartInfo.EnvironmentVariables["LC_ALL"] = "en_US.UTF-8";
myprocess.StartInfo.EnvironmentVariables["__POWERSHELL_UI_CULTURE"] = "en-US";
myprocess.StartInfo.EnvironmentVariables["__POWERSHELL_CULTURE"] = "en-US";

myprocess.Start();
String mystring= myprocess.StandardOutput.ReadToEnd();
myprocess.WaitForExit();

注意事项

如果以上方案都不生效,请先确认你的Windows系统已经安装了en-US对应的UI语言包,没有安装语言包的情况下强制修改文化配置不会生效。

内容的提问来源于stack exchange,提问作者Test

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 14:57:04