运行 来自我的 C# 代码的 cmd 命令

Running a cmd command from within my C# code

我目前在托管 OSRM locally on my machine to build a routing application. When the application starts, a bool ServiceAvailable is checked with a test query to see if the application is available and running locally. I want to be able to start the OSRM application should this bool return false. I found a Whosebug link 时遇到类似问题并尝试实施它,但应用程序无法加载。这是我当前的代码:

    private void StartOSRMService()
    {
        Process process = new Process();
        process.StartInfo.WorkingDirectory = @"C:\";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = "/c cd users/james/desktop/osrm/osrm-backend/osrm_release";
        process.StartInfo.Arguments = "/c osrm-routed wales-latest.osrm";
    }

方法是运行,但服务从未启动。在其他方法中,由于缺少服务,我的代码由于 Http.Web 请求错误而中断。

您可以尝试以下方法:

    private void StartOSRMService()
    {
        var startInfo = new ProcessStartInfo(@"C:\users\james\desktop\osrm\osrm-backend\osrm_release\osrm-routed.exe");
        startInfo.WorkingDirectory = @"C:\users\james\desktop\osrm\osrm-backend\osrm_release";
        startInfo.UseShellExecute = false;
        startInfo.Arguments = "wales-latest.osrm";
        Process.Start(startInfo);
    }

有关 Process.Start()

的更多信息

此外,根据您原来的 StartInfo.Arguments,“/C”告诉控制台在命令执行后终止,因此,如果“osrm-routed”是需要在控制台中 运行 的服务,并且控制台终止,那么应用程序本身也会在控制台终止时终止。