Launch external process (.exe) from asp.net core app

10,839

First, it is a very bad idea to spin up an external process like this. So please, don't do this in a real application. You will more than likely create far more issues and security holes that it would ever be worth. There are several, far more robust, architectural patterns for dealing with external processes outside your request pipeline.

That said, the problem here is that calc.exe is failing to launch on your server. Your method doesn't know about this however since you're simply telling it to start a Process, you're not checking to see what state that process is in.

var process = Process.Start("C:\\Windows\\System32\\calc.exe");
if (process == null) // failed to start
{
    return InternalServerError();
}
else // Started, wait for it to finish
{
    process.WaitForExit();
    return Ok();
}
Share:
10,839
Zoinky
Author by

Zoinky

Updated on June 05, 2022

Comments

  • Zoinky
    Zoinky almost 2 years

    I have the following

    [HttpPost]
    public IActionResult LaunchExternalProcess()
    {
        Process.Start("C:\\Windows\\System32\\calc.exe");
        return Ok();
    
    }
    

    And this works perfectly fine on my local machine, but when deployed onto IIS 10 (windows 2016) I get no errors but it does not launch the calc on the server.

    I simply want to call an external .exe from a button on my page.

    Here is the javascript that I am using which also works on my local but no errors on server and it displays the success message

    $.ajax({
        url: "/Admin/LaunchExternalProcess",
        type: "POST",
        cache: false,
    
        success: function () {
    
            console.log("success");
        }
    });