Process.Start()不适用于gcc.exe / g ++。exe

时间:2016-05-09 22:23:35

标签: c# winforms gcc process g++

好吧,标题几乎告诉它: 我在使用Process.Start()获取

时遇到了麻烦
        
  • gcc.exe
  •     
  • 克++。exe文件

但它对其他一切都很好。

我用来设置我的流程的代码(编辑:已更新为完整代码):

NSObject

private void Compile(string path, string exe) { ProcessStartInfo startInfo = new ProcessStartInfo();

        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardOutput = true;

        if (path.EndsWith(".cpp") || path.EndsWith(".cxx") || path.EndsWith(".cc"))
        {
            startInfo.WorkingDirectory = Path.Combine(Application.StartupPath, @"compiler\MinGW64\bin");
            startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\MinGW64\bin"), "g++.exe");

            if (exe.EndsWith(".exe"))
                startInfo.Arguments = string.Concat('"', path, '"') + " -o " + string.Concat('"', exe, '"') + " -Wall -Wextra -std=c++11";
            else
                startInfo.Arguments = string.Concat('"', path, '"') + " -o " + string.Concat('"', exe, ".exe", '"') + " -Wall -Wextra -std=c++11";
        }
        if (path.EndsWith(".c"))
        {
            startInfo.WorkingDirectory = Path.Combine(Application.StartupPath, @"compiler\MinGW64\bin");
            startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\MinGW64\bin"), "gcc.exe");

            if (exe.EndsWith(".exe"))
                startInfo.Arguments = string.Concat('"', path, '"') + " -o " + string.Concat('"', exe, '"') + " -Wall -Wextra -std=c11";
            else
                startInfo.Arguments = string.Concat('"', path, '"') + " -o " + string.Concat('"', exe, ".exe", '"') + " -Wall -Wextra -std=c11";
        }
        if (path.EndsWith(".cs"))
        {
            startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\v4.0.30319"), "csc.exe");

            if (exe.EndsWith(".exe"))
                startInfo.Arguments = "/out:" + string.Concat('"', exe, '"') + " /target:exe " + string.Concat('"', path, '"');
            else
                startInfo.Arguments = "/out:" + string.Concat('"', exe, ".exe", '"') + " /target:exe " + string.Concat('"', path, '"');
        }
        if(path.EndsWith(".vb"))
        {
            startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\v4.0.30319"), "vbc.exe");

            if(exe.EndsWith(".exe"))
                startInfo.Arguments = "/out:" + string.Concat('"', exe, '"') + " /target:exe " + string.Concat('"', path, '"');
            else
                startInfo.Arguments = "/out:" + string.Concat('"', exe, ".exe", '"') + " /target:exe " + string.Concat('"', path, '"');
        }
        if(path.EndsWith(".il"))
        {
            startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\v4.0.30319"), "ilasm.exe");

            if (exe.EndsWith(".exe"))
                startInfo.Arguments = string.Concat('"', path, '"') + " /EXE /OUTPUT:" + string.Concat('"', exe, '"');
            else
                startInfo.Arguments = string.Concat('"', path, '"') + " /EXE /OUTPUT:" + string.Concat('"', exe, ".exe", '"');
        }
        #endregion

        #region Launching compiler and redirect its stdout to a TextBox
        TCCompilerOutputOutputTextBox.Text += string.Concat(startInfo.FileName, ' ', startInfo.Arguments, "\r\n");

        try
        {
            using (Process comp = new Process()
            { StartInfo = startInfo, EnableRaisingEvents = true } )
            {
                try
                {
                    comp.ErrorDataReceived += Comp_InterceptOutput;
                    comp.OutputDataReceived += Comp_InterceptOutput;
                    comp.Start();

                    comp.BeginErrorReadLine();
                    comp.BeginOutputReadLine();

                    comp.WaitForExit();

                    TCCompilerOutputOutputTextBox.AppendText(OutputBuilder.ToString());
                    OutputBuilder.Clear();

                    TimeSpan time = comp.TotalProcessorTime;
                    var flag = !Convert.ToBoolean(comp.ExitCode);
                    if (flag)
                    {
                        TCCompilerOutputOutputTextBox.AppendText("\r\n\r\nCompilation finished after " + time + "\r\n" +
                            "--------------------------------------------------------------------------------\r\n\r\n");
                    }
                    else
                    {
                        TCCompilerOutputOutputTextBox.AppendText("\r\n\r\nCompilation failed!\r\n" +
                            "--------------------------------------------------------------------------------\r\n\r\n");
                    }
                }
                catch (Exception ex)
                {
                    var bfr = new ExceptionInformer(ex);

                    bfr.ShowDialog();
                }
            }
        }
        catch(Exception ex)
        {
            var bfr = new ExceptionInformer(ex);

            bfr.ShowDialog();
        }
    }

我也尝试设置工作目录,但没有任何改变。


可变含义:

  • path(string):要编译的文件的完整路径
  • exe(字符串):已编译输出的完整路径


PS:对不起,如果我忘记了任何必要的信息,只要问你是否遗漏了某些内容:)

编辑:'麻烦'意思是,gcc.exe没有启动,但其他一切都没有。

解决方案:我终于让它发挥作用了! gcc / g ++抱怨缺少DLL,所以我有了另外设置工作目录的想法(首先我认为只有将UseShellExecute设置为' true'才有必要)。 更新为工作代码。

0 个答案:

没有答案