为什么我在processinfo上获取System.ComponentModel.Win32Exception以及如何解决它?

时间:2015-12-20 00:37:23

标签: c# .net winforms

在我的form1顶部:

string InputFile = @"e:\lightningsmov\MVI_7909.MOV";
string OutputFile = @"e:\lightningsmov\MVI_7909.mp4";
string cmd;
string exepath = @"E:\myffmpegstatic\ffmpeg-20151217-git-9d1fb9e-win64-static\bin\ffmpeg.exe";
string placeholder;

在构造函数中:

label2.Text = InputFile;
cmd = " -i \"" + InputFile + "\" \"" + OutputFile + "\"";
initProgressBar();

initProgressBar:

private void initProgressBar()
        {
            progressBar1.Style = ProgressBarStyle.Continuous;
            // for every line written to stdOut, raise a progress event
            int result = SpawnProcessSynchronous(InputFile, cmd, out placeholder, false,
                (sender, eventArgs) =>
                {
                    if (eventArgs.Data.StartsWith("TotalSteps="))
                    {
                        progressBar1.Minimum = 0;
                        progressBar1.Maximum = Convert.ToInt32(eventArgs.Data.Replace("TotalSteps=", ""));
                        progressBar1.Value = 0;
                    }
                    else
                    {
                        progressBar1.Increment(1);
                    }
                });
        }

和SpawnProcessSynchronous:

public static int SpawnProcessSynchronous(string fileName, string args, out string stdOut, bool isVisible, DataReceivedEventHandler OutputDataReceivedDelegate)
        {
            int returnValue = 0;
            var processInfo = new ProcessStartInfo();
            stdOut = "";
            processInfo.FileName = fileName;
            processInfo.WorkingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ?? "";
            //log.Debug("Set working directory to: {0}", processInfo.WorkingDirectory);

            processInfo.WindowStyle = isVisible ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
            processInfo.UseShellExecute = false;
            processInfo.RedirectStandardOutput = true;
            processInfo.CreateNoWindow = true;

            processInfo.Arguments = args;
            using (Process process = Process.Start(processInfo))
            {
                if (OutputDataReceivedDelegate != null)
                {
                    process.OutputDataReceived += OutputDataReceivedDelegate;
                    process.BeginOutputReadLine();
                }
                else
                {
                    stdOut = process.StandardOutput.ReadToEnd();
                }
                // do not reverse order of synchronous read to end and WaitForExit or deadlock
                // Wait for the process to end.  
                process.WaitForExit();
                returnValue = process.ExitCode;
            }
            return returnValue;
        }

该行的异常是在SpawnProcessSynchronous中:

using (Process process = Process.Start(processInfo))

指定的可执行文件不是此OS平台的有效应用程序

堆栈跟踪:

at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at ConvertVideo.Form1.SpawnProcessSynchronous(String fileName, String args, String& stdOut, Boolean isVisible, DataReceivedEventHandler OutputDataReceivedDelegate) in d:\C-Sharp\ConvertVideo\ConvertVideo\ConvertVideo\Form1.cs:line 88

第88行是:

using (Process process = Process.Start(processInfo))

1 个答案:

答案 0 :(得分:1)

我认为您已经搞砸了流程启动信息参数。

int result = SpawnProcessSynchronous(InputFile, cmd, ...

应该是

int result = SpawnProcessSynchronous(exepath, cmd,