【问题标题】:run linux executable in c# with params使用参数在 c# 中运行 linux 可执行文件
【发布时间】:2020-11-23 23:53:57
【问题描述】:

我需要在 c# 中使用运行时参数运行一个不带扩展名的 linux 文件,我想知道您是否可以直接使用某些函数来执行此操作,或者您需要在代码中编写运行时 bash 脚本。

示例:

运行 c:/app -port=(更改代码中的端口变量)

【问题讨论】:

  • 查看System.Diagnostics.Process docs.microsoft.com/en-us/dotnet/api/…
  • 你是如何运行这个过程的?
  • 既然你在 Linux 上,你使用的是 .NET Core,Mono 吗?我想没关系。我会写一个例子。

标签: c# linux parameters executable


【解决方案1】:

这是使用参数启动终端应用程序的典型方式,等待它完成,然后将发送的内容输出到标准输出。

using(var p = Process.Start(new ProcessStartInfo
{
    FileName = "/bin/ls", // File to execute
    Arguments = "~/Documents", // arguments to use
    UseShellExecute = false, // use process creation semantics
    RedirectStandardOutput = true, // redirect standard output to this Process object
    CreateNoWindow = true, // if this is a terminal app, don't show it
    WindowStyle = ProcessWindowStyle.Hidden // if this is a terminal app, don't show it
})
{
    // Wait for the process to finish executing
    p.WaitForExit();
    // display what the process output
    Console.WriteLine(p.StandardOutput.ReadToEnd());
}

【讨论】:

  • 所以对于我来说,我会使用“/home/program”作为文件名并使用“-port=777”作为参数?这是正确的语法吗?
  • @fesdfadfa -- 是的,你就是这样做的
  • 我试过了,它只是说本地错误=找不到指定的文件。我确定文件确实在那里,我仍然对无扩展名文件感到困惑,也许这不是执行它们的正确方法?我的输入:ApplicationName='/home/ubuntu/.steam/SteamApps/common/'Retail Royale Server'/LinuxServer/IkeaBR_Server/Binaries/Linux/IkeaBR_ServerServer', CommandLine='-log -queryport=27020 -port=7780', CurrentDirectory=''
  • 顺便说一句,通过终端使用 ./IkeaBR_ServerServer 运行此文件
猜你喜欢
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 2019-09-27
  • 2018-05-02
  • 1970-01-01
相关资源
最近更新 更多