C#中ProcessStartInfo的Arguments参数未生效,外部进程输出无法正常捕获的问题求助
C#中ProcessStartInfo的Arguments参数未生效,外部进程输出无法正常捕获的问题求助
各位好,我遇到一个关于C#调用外部进程的问题,折腾了好久没搞明白,想请教下大家。
问题场景
我写了一段C#代码,通过ProcessStartInfo调用本地的mysample.exe,并传递一个API地址作为参数,期望能捕获到这个exe执行后的输出(直接在命令行运行它会返回500 : (500) Internal Server Error https://sample.domain.co.jp/api/test),但实际代码返回的输出是空字符串,完全没拿到预期的错误信息。
我的代码
这是我用来调用外部进程的C#接口代码:
[HttpGet("tickets")] public IActionResult GetTickets() { var startInfo = new ProcessStartInfo { FileName = @".\mysample.exe", Arguments = @"https://sample.domain.co.jp/api/test", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = false }; var process = new Process { StartInfo = startInfo }; process.Start(); process.WaitForExit(); string output = process.StandardOutput.ReadToEnd(); ApiTicketsResponse response = new ApiTicketsResponse(); response.code = 1; response.message = output; return Json(response); }
测试情况
- 我通过curl调用这个接口:
返回结果是:curl -X 'GET' \ 'https://localhost:7244/MfApi/tickets' \ -H 'accept: */*'{ "code": 1, "message": "" } - 但直接在PowerShell里运行
.\mysample.exe https://sample.domain.co.jp/api/test,能正常得到输出:500 : (500) Internal Server Error https://sample.domain.co.jp/api/test
我的疑问
为什么代码里调用时,参数好像没生效?或者说为什么捕获不到外部进程的输出?有没有可能是输出流读取的时机不对?还是参数传递有问题?
麻烦各位大佬帮忙分析下根因,谢谢了!
备注:内容来源于stack exchange,提问作者Heisenberg
相关产品推荐
相关产品推荐

