c# - C# 使用 Process.Start() 作为 cmd 和活动 RedirectStandardOutput 的参数执行应用程序
问题描述
我正在使用 Visual Studio Clickones 并尝试执行(.appref-ms) application
和使用RedirectStandardOutput
...
我必须使用该选项"Prefer 32-bit"
,因为我正在使用带有连接字符串的 Access DBProvider=Microsoft.Jet.OLEDB.4.0.
这是我的执行代码:
var p = new Process();
p.StartInfo = new ProcessStartInfo(@"C:\Users\hed-b\Desktop\PulserTester.appref-ms")
{
RedirectStandardOutput = true,
UseShellExecute = false
};
p.Start();
reportNumber = p.StandardOutput.ReadToEnd();
p.WaitForExit();
这是我得到的错误
System.ComponentModel.Win32Exception:“指定的可执行文件不是此 OS 平台的有效应用程序。”
编辑
通过看这里,我看到我可以通过 cmd 运行它
.Net Core 2.0 Process.Start 抛出“指定的可执行文件不是此 OS 平台的有效应用程序”
作为
var proc = Process.Start(@"cmd.exe ",@"/c C:\Users\hed-b\Desktop\PulserTester.appref-ms")
但是我怎样才能以这种方式使用 RedirectStandardOutput 呢?
解决方案
看起来您打算启动 CMD 并运行命令,但您的代码只是尝试将该命令作为应用程序运行。
尝试这样的事情。
var p = new Process();
p.StartInfo = new ProcessStartInfo(@"cmd.exe")
{
RedirectStandardOutput = true,
UseShellExecute = false,
Arguments = "/c C:\Users\hed-b\Desktop\PulserTester.appref-ms"
};
p.Start();
reportNumber = p.StandardOutput.ReadToEnd();
p.WaitForExit();
使用阻塞ReadToEnd
将在该代码运行时暂停您的线程,并且也更难捕获错误输出 - 查看此答案以演示捕获标准数据和标准错误的非阻塞解决方案:ProcessInfo 和 RedirectStandardOutput
推荐阅读
- javascript - 如何使用 jQuery 获取每个元素的索引号
- docker - 在docker中以supervisord作为CMD运行Lucee(tomcat),奇怪的路径问题
- android - Trying to create a PDF from a listview
- emacs - 从项目中打开一个固定文件(在 Emacs Projectile 中)
- powershell - 如何过滤我导出的 DHCP 租约信息
- python - 使用Django模板中另一个字典中的键访问字典
- python-3.x - 基于符号、日期、平均价格的股票预测
- java - 用于 Spring 存储库的自定义 ResourceAssembler
- java - 无法在 .jar 构建中包含本地 maven 依赖项
- c - C从字符到函数名