繁体   English   中英

System.dll中发生Process.Start()'System.ComponentModel.Win32Exception'

[英]Process.Start() 'System.ComponentModel.Win32Exception' occurred in System.dll

我试图订阅process.OutputDataReceived。 看不到结果(事件未在输出中触发),我在Msdn上跟踪了一个示例。 但是process.BeginOutputReadLine(); 给我例外。 我试图运行的进程是一个简单的批处理文件命令wmic product where "Name like '%%Microsoft Visual C++ %%'" get Name, Version与此process.Exited相符就足够了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"; }));
    }

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

PS省略线

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

触发批处理文件。 因此,我相信它可以找到该文件。

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

因此,例如:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM