【问题标题】:Process.Start() 'System.ComponentModel.Win32Exception' occurred in System.dllSystem.dll 中发生 Process.Start() 'System.ComponentModel.Win32Exception'
【发布时间】:2017-05-19 09:24:26
【问题描述】:

我正在尝试订阅 process.OutputDataReceived。看不到任何结果(事件未在输出中触发)我按照Msdn 上的示例进行操作。但是process.BeginOutputReadLine();给了我一个例外。我试图运行的过程是一个简单的批处理文件命令wmic product where "Name like '%%Microsoft Visual C++ %%'" get Name, Version 奇怪的是process.Exited 事件工作得很好。

这里是代码

  void DoSomething(object sendingProcess, DataReceivedEventArgs outLine)
    {
        Application.Current.Dispatcher.Invoke(new Action(() => { textBox.Text = outLine.Data; }));
    }
    public MainWindow()
    {
        InitializeComponent();

        string batPath = @"..\..\WMIC batch\";

        var process = new Process();

        process.StartInfo.WorkingDirectory = batPath;
        process.StartInfo.FileName = "Redistributable_Packages_Check.bat";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;

        process.EnableRaisingEvents = true;
        process.Exited += new EventHandler(Process_Exited);

        process.OutputDataReceived +=new DataReceivedEventHandler(DoSomething);

        process.StartInfo.RedirectStandardInput = true;
        process.Start();

        StreamWriter sortStreamWriter = process.StandardInput;
        process.BeginOutputReadLine();

        //textBox.Text = "Initialised";



    }

    private void Process_Exited(object sender, EventArgs e)
    {
        Application.Current.Dispatcher.Invoke(new Action(() => { textBox.Text = "Process has exited"; }));
    }

异常详情{“系统找不到指定的文件”}

P.S 省略行

  process.StartInfo.UseShellExecute = false;
  process.StartInfo.RedirectStandardOutput = true;
  process.StartInfo.RedirectStandardInput = true;

触发批处理文件。所以我相信它可以找到文件。

【问题讨论】:

  • 当你尝试在 cmd 中运行命令时会发生什么?
  • 尝试指定批处理文件的绝对路径:Process.Start("c:\yourfolder\WMIC batch\Redistributable_Packages_Check.bat");
  • @C0d1ngJammer 在 cmd 中运行命令时,会显示系统上安装的 Microsoft 软件包。因此,我的系统 30 个结果中的前 4 个结果是:Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.4974 9.0.30729.4974,Microsoft Visual C++ 2013 x86-x64 Compilers 12.0.21005,Microsoft Visual C++ 2015 x64 Debug Runtime - 14.0.24215 14.0.24215,Microsoft Visual C++ 2010 x64 Redistributable - 10.0.40219 10.0.40219,

标签: c# wpf


【解决方案1】:

我创建了一个像你这样的小例子,发现原因确实是(如 cmets 中提到的 mm8)你需要为 process.StartInfo.FileName 指定 完整路径。设置WorkingDirectory是不够的。

例如:

process.StartInfo.WorkingDirectory = batPath;
process.StartInfo.FileName = System.IO.Path.Combine(batPath, "Redistributable_Packages_Check.bat");

WorkingDirectory 仅指定新进程 使用的相对文件名相关的路径。但是FileName 仍然是相对于您当前进程的工作目录的。

【讨论】:

    猜你喜欢
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    相关资源
    最近更新 更多