将标准输出从进程 (msxsl.exe) 重定向到 VB.NET 中的字符串
我正在 VB.NET 中编写一个命令行应用程序。此应用程序正在调用另一个应用程序 msxsl.exe 来运行 XSL 转换。我正在使用 Process 类来执行此操作:
Dim process = New Process()
process.StartInfo.FileName = "msxsl.exe"
process.StartInfo.Arguments = "base.xml test.xsl -o styled.xml"
process.StartInfo.UseShellExecute = False
process.StartInfo.CreateNoWindow = True
process.StartInfo.RedirectStandardOutput = True
process.Start()
这部分效果很好。我希望它能够将此进程的输出显示到我的应用程序的控制台。我读过几篇解释此方法的帖子,但在这种情况下似乎不起作用。输出是一个空字符串。
Dim output As String = process.StandardOutput.ReadToEnd()
process.WaitForExit()
Console.WriteLine(output)
我已经验证,如果我单独运行 msxsl 可执行文件(即运行“msxsl.exe base.xml test.xsl -o styled.xml”),它会在命令行上显示输出。我做错了什么?
编辑:我应该注意到,由于 XML 文件格式错误,msxsl 进程当前失败。它显示此错误消息:
执行样式表“test.xsl”时发生错误。
代码:0x800c0006
系统找不到指定的对象。
这正是我想要在应用程序的控制台(或者最终是日志文件)中显示的内容类型。
I am writing a command line application in VB.NET. This application is calling another one, msxsl.exe, to run an XSL transform. I am using the Process class to do this:
Dim process = New Process()
process.StartInfo.FileName = "msxsl.exe"
process.StartInfo.Arguments = "base.xml test.xsl -o styled.xml"
process.StartInfo.UseShellExecute = False
process.StartInfo.CreateNoWindow = True
process.StartInfo.RedirectStandardOutput = True
process.Start()
This part works great. What I want it to be able to display the output from this process to the console of my application. I have read several posts explaining this method, but it does not seem to work in this case. The output is an empty string.
Dim output As String = process.StandardOutput.ReadToEnd()
process.WaitForExit()
Console.WriteLine(output)
I have verified that if I run the msxsl executable on its own (i.e. running "msxsl.exe base.xml test.xsl -o styled.xml"), it displays output on the command line. What am I doing wrong?
EDIT: I should note that the msxsl process is currently failing due to a malformed XML file. It is displaying this error message:
Error occurred while executing stylesheet 'test.xsl'.
Code: 0x800c0006
The system cannot locate the object specified.
This is exactly the type of thing I want displayed in the console of my application (or, eventually, a log file.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为这不是标准输出,而是
StandardError
,您需要像这样Process.StartInfo.RedirectStandardError = True
重定向StandardError
和然后将其读入字符串。Dim ErrorString As String = Process.StandardError.ReadToEnd()
This is probably because this isn't standard output it is
StandardError
you will want to redirectStandardError
like soProcess.StartInfo.RedirectStandardError = True
and then read that into a string.Dim ErrorString As String = Process.StandardError.ReadToEnd()