【问题标题】:Executing an command in Azure Bash from C#从 C# 在 Azure Bash 中执行命令
【发布时间】:2019-05-06 15:21:25
【问题描述】:

我有一个需要通过 C# 程序管理的 Azure 环境。为了正确管理环境,我需要执行一些 .sh 文件。 我想制定一个例程,以便在一定时间后执行命令并使用一些参数。目前我正在从命令行手动执行一个 .sh 文件。

我正在寻找一种通过 C# 调用 .sh 文件的方法。但是我找不到任何关于此的文档。这可能吗?

【问题讨论】:

  • 嗯,你需要告诉你想在 Azure 环境中在哪里运行 bash 文件?

标签: c# bash azure .net-core


【解决方案1】:

在 C# 中没有执行 linux 命令的“exec”命令。相反,您可以使用“System.Diagnostics.ProcessStartInfo”和“System.Diagnostics.Process”。由于您需要运行 shell 命令,因此无法使用 cmd ,因此您可以使用 cygwin 作为应用程序来执行它们。

因此这段代码有效,

ProcessStartInfo procStartInfo = new ProcessStartInfo(@"C:\cygwin\bin\bash.exe",command);

然后

proc.StartInfo = procStartInfo;

proc.Start();

完整的代码如下:

private void runCom(string command)
{
    ProcessStartInfo procInfo = new ProcessStartInfo("cmd", "/c" + command);
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    Process proc = new Process();
    proc.StartInfo = startInfo;
    proc.Start();
    proc.WaitForExit();
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2018-10-17
    • 2012-04-05
    • 1970-01-01
    • 2015-06-28
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多