ASP.NET Core 中调用 第三方应用

2019年12月14日 3018点热度 0人点赞 0条评论
内容目录

public class ValuesController : ControllerBase
{
    // GET api/values/123
    [HttpGet("{id}")]
    public ActionResult<string> Get(int id)
    {
        using (var process = new Process())
        {
            process.StartInfo.FileName = @"..\HelloWorld\bin\Debug\helloworld.exe"; // relative path. absolute path works too.
            process.StartInfo.Arguments = $"{id}";
            //process.StartInfo.FileName = @"cmd.exe";
            //process.StartInfo.Arguments = @"/c dir";      // print the current working directory information
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;

            process.OutputDataReceived += (sender, data) => Console.WriteLine(data.Data);
            process.ErrorDataReceived += (sender, data) => Console.WriteLine(data.Data);
            Console.WriteLine("starting");
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            var exited = process.WaitForExit(1000 * 10);     // (optional) wait up to 10 seconds
            Console.WriteLine($"exit {exited}");
        }
        return "value";
    }

原文地址 https://codeburst.io/run-an-external-executable-in-asp-net-core-5c2f8b6cacd9

痴者工良

高级程序员劝退师