将标准输出从进程 (msxsl.exe) 重定向到 VB.NET 中的字符串

发布于 2024-10-04 08:51:04 字数 914 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖哒 2024-10-11 08:51:04

这可能是因为这不是标准输出,而是 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 redirect StandardError like so Process.StartInfo.RedirectStandardError = True and then read that into a string.

Dim ErrorString As String = Process.StandardError.ReadToEnd()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文