gpt4 book ai didi

c# - Mono:如何在新的控制台/终端窗口上运行 bash 命令?

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:22 24 4
gpt4 key购买 nike

如何在新的控制台/终端窗口中使用 Mono (System.Diagnostics.Process) 运行 bash 命令?它在 Windows 上运行良好,会打开一个新的控制台窗口来运行该命令。在 Linux 和 macOS 上,该命令在我用来打开应用程序的同一终端窗口上运行。我已经尝试了所有 CreateNoWindow/UseShellExecute 组合,但没有一个对我有用。

编辑:这是代码:

                    var startInfo = new ProcessStartInfo();
switch (ThermoCS.PlatformCheck.RunningPlatform())
{
case ThermoCS.PlatformCheck.Platform.Windows:
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Environment.CurrentDirectory + "\\ThermoCS\\" + item.Key + ".exe";
if (item.Key.Contains("1"))
{
startInfo.Arguments = Model;
}
else
{
startInfo.Arguments = Model + " " + MixRule;
}
break;
case ThermoCS.PlatformCheck.Platform.Linux:
startInfo.WorkingDirectory = Environment.CurrentDirectory;
var ldc = "LD_LIBRARY_PATH=" + Environment.CurrentDirectory + "/ThermoCS/; export LD_LIBRARY_PATH";
//startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "/bin/bash";
if (item.Key.Contains("1"))
{
startInfo.Arguments = "-c \" " + ldc + " && chmod +x ThermoCS/" + item.Key + " && ./ThermoCS/" + item.Key + " " + Model + " \"";
}
else
{
startInfo.Arguments = "-c \" " + ldc + " && chmod +x ThermoCS/" + item.Key + " && ./ThermoCS/" + item.Key + " " + Model + " " + MixRule + " \"";
}
break;
case ThermoCS.PlatformCheck.Platform.Mac:
var basedir = Directory.GetParent(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)).Parent.FullName;
var ldcosx = "export DYLD_LIBRARY_PATH=" + basedir + "/Contents/MonoBundle/ThermoCS/";
startInfo.WorkingDirectory = basedir;
//startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "/bin/bash";
if (item.Key.Contains("1"))
{
startInfo.Arguments = "-c \" " + ldcosx + " && chmod +x Contents/MonoBundle/ThermoCS/" + item.Key + " && ./Contents/MonoBundle/ThermoCS/" + item.Key + " " + Model + " \"";
}
else
{
startInfo.Arguments = "-c \" " + ldcosx + " && chmod +x Contents/MonoBundle/ThermoCS/" + item.Key + " && ./Contents/MonoBundle/ThermoCS/" + item.Key + " " + Model + " " + MixRule + " \"";
}
break;
}
Process proc = Process.Start(startInfo);

正如我上面所描述的,该命令运行得很好。问题在于,在 Linux 和 macOS 上,它在拥有当前运行的应用程序的同一终端窗口上执行。我希望它在新的终端窗口上运行。

提前致谢!

最佳答案

我所做的是编写了一个简单的 bash 脚本,并将其放置在 Debug/Release/Whatever 文件夹(C# 程序的可执行文件的文件夹)中。让我们调用 bash 脚本,first.sh

在此 bash 文件夹中,我将从另一个目录调用另一个 bash,即我最初希望运行的 bash。我们将此 bash 脚本称为 Second.sh

在我的 C# 代码中,我将像这样调用first.sh:

 string command = string.Format("{0}", "./first.sh");
Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "gnome-terminal";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardInput = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.Arguments = " -e \" " + command + " \"";

proc.Start();

我的first.sh将更改为我的second.sh所在的目录:

#!/bin/sh
# This is a comment!
echo hello world # This is just a test to see if it is being called
cd ~
cd home/scripts
(exec "./second")

整个过程将物理上打开一个新终端并从 C# 代码执行其中的命令。

这里有一个关于如何编写 bash 脚本的很好的教程:tutorial on bash scripting in linux

关于c# - Mono:如何在新的控制台/终端窗口上运行 bash 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46323730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com