运行 来自 C# 的 C 代码

run C code from C#

有什么方法可以从 C# 代码调用 C 代码吗?

看了很多微软文档,暂时尝试了这种方式:

Process proc = new Process();
proc.StartInfo.WorkingDirectory = "path-to-C-code";
proc.StartInfo.FileName = "C-code-name";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
Console.WriteLine(proc.StandardOutput.ReadToEnd());
proc.WaitForExit();

但显然是行不通的。

无论我做什么,这个字符串都保持为空 proc.StartInfo.FileName

这是 C 代码,该代码用于杀死自己的进程。

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[], char *env[]) {

  int value1;
  int value2;

  srandom(time(NULL));
  switch (random() % 7) {
  case 0:
    exit(random() % 10);
    break;

  case 1:
    value1 = value1 / (value2 - value2);
    break;

  case 2:
    kill(getpid(), SIGKILL);
    break;

  case 3:
    alarm(random() % 60);
    break;

  case 4:
    __asm__("sti");
    break;

  case 5:
    value1 = *((int*) NULL);
    break;

  default:
    break;
  }

  return 0;
}

提前致谢。

using System;
using System.Diagnostics;

namespace runGnomeTerminal
{
    class MainClass
    {
        public static void ExecuteCommand(string command)
        {
            Process proc = new System.Diagnostics.Process ();
            proc.StartInfo.FileName = "/bin/bash";
            proc.StartInfo.Arguments = "-c \" " + command + " \"";
            proc.StartInfo.UseShellExecute = false; 
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start ();

            while (!proc.StandardOutput.EndOfStream) {
                Console.WriteLine (proc.StandardOutput.ReadLine ());
            }
        }

        public static void Main (string[] args)
        {
            ExecuteCommand("gnome-terminal -x bash -ic 'cd $HOME; ls; bash'");
        }


    }
}

感谢 J.Piquard