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

.NET Framework 4.8窗体程序调用es.exe子进程输出乱码求助

解决.NET Framework调用Everything es.exe输出乱码问题

问题根源

es.exe作为Windows控制台程序,输出编码采用GBK(代码页936),你的代码未指定读取子进程输出的编码,.NET默认使用UTF-8解析,导致GBK编码的中文被错误解码为乱码。

修复方案

在ProcessStartInfo中明确指定输出和错误流的编码为GBK(代码页936),同时保持直接调用es.exe的方式(无需通过cmd.exe中转,避免额外的引号处理问题)。

修改后的完整代码:

Imports System.Text
Imports System.Diagnostics

Function ExecuteCommand(command As String, args As String) As String
    Dim processStartInfo As New ProcessStartInfo()
    processStartInfo.FileName = command
    processStartInfo.Arguments = args
    processStartInfo.RedirectStandardOutput = True
    processStartInfo.RedirectStandardError = True
    processStartInfo.UseShellExecute = False
    processStartInfo.CreateNoWindow = True
    
    ' 关键:设置编码为GBK(代码页936),匹配es.exe的输出编码
    processStartInfo.StandardOutputEncoding = Encoding.GetEncoding(936)
    processStartInfo.StandardErrorEncoding = Encoding.GetEncoding(936)

    Dim process As New Process()
    process.StartInfo = processStartInfo

    Dim output As String
    Dim errorOutput As String

    Try
        process.Start()
        output = process.StandardOutput.ReadToEnd()
        errorOutput = process.StandardError.ReadToEnd()
        process.WaitForExit()
    Catch ex As Exception
        Return $"An error occurred: {ex.Message}"
    End Try

    Return output & Environment.NewLine & errorOutput
End Function

补充说明

  • 带空格的路径无需手动加双引号:ProcessStartInfo.FileName会自动处理路径中的空格,直接传入完整路径即可。
  • 若仍需通过cmd.exe中转,需确保路径的双引号正确包裹,但直接调用es.exe是更简洁可靠的方式。

内容的提问来源于stack exchange,提问作者Dude named Ben

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 10:32:37