首页 > 解决方案 > 从 C# 将 ngrok 控制台应用程序作为进程运行不起作用

问题描述

亲爱的 Stack Overflow 社区,

我正在尝试使用 C# 与 Ngrok 控制台进行通信。不幸的是,“StartInfo.Arguments”不起作用。比如我在c#代码中写“StartInfo.Arguments=" ngrok”,没有出现ngrok帮助文本,但是日志中出现了“ERROR: Unrecognized command: ngrok”。但是如果我自己打开控制台写进去“ ngrok”它有效。

private void startServer()

        Process compiler = new Process();
        compiler.StartInfo.FileName = "ngrok.exe";
        compiler.StartInfo.Arguments = "\"ngrok\"";
        compiler.StartInfo.UseShellExecute = false;
        compiler.StartInfo.RedirectStandardOutput = true;
        compiler.Start();

        Console.WriteLine(compiler.StandardOutput.ReadToEnd());

        compiler.WaitForExit();
    }

标签: c#.netprocessconsolengrok

解决方案


您正在使用“ngrok”作为参数。这与您ngrok.exe ngrok在控制台中编写的内容相同。ngrok 无法识别该命令。例如,尝试使用适当的参数compiler.StartInfo.Arguments = "http 80";或将其留空。如果您想通过 http 使用端口 80 的 ngrok,您的代码必须如下所示:

private void startServer()

    Process compiler = new Process();
    compiler.StartInfo.FileName = "ngrok.exe";
    compiler.StartInfo.Arguments = "http 80";
    compiler.StartInfo.UseShellExecute = false;
    compiler.StartInfo.RedirectStandardOutput = true;
    compiler.Start();

    Console.WriteLine(compiler.StandardOutput.ReadToEnd());

    compiler.WaitForExit();
}

推荐阅读