Jay Gould Jay Gould - 7 months ago 43
C# Question

Running a cmd command from within my C# code

I'm currently hosting 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
return false. I found a StackOverflow link with a similar issue and tried to implement it, but the application doesn't load. Here's my current code:

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";

The method is ran but the service never starts. In other methods, my code breaks due to a
request error, due to the lack of the service.


You can try the following:

    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";

More info on Process.Start()

Also, based on your original StartInfo.Arguments, the "/C" tells to console to terminate after the command has been executed, thus, if the "osrm-routed" is the service that needs to run in the console, and the console is terminated, then the application itself will also terminate when the console terminates.