【问题标题】:Passing arguments to Terminal on Mac via C# in Visual Studio在 Visual Studio 中通过 C# 将参数传递给 Mac 上的终端
【发布时间】:2021-06-05 19:13:26
【问题描述】:

我在通过 C# 中的 Visual Studio 向终端传递参数时遇到了一些问题。

我可以成功打开终端,但是我传递的参数似乎被忽略了。

一般信息:

  • Visual Studio for Mac 版本 8.9 build 1651
  • 解决方案 = Cocoa 应用程序
  • 使用 Xamarin/Xcode
  • 当我打开终端并自己输入时,终端命令可以正常工作

这是我的代码:

var encode = new System.Diagnostics.Process();
encode.StartInfo.FileName = ("/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal");
encode.StartInfo.Arguments = ("ffmpeg -i " + filepath + " -acodec copy file_fixed.mp3");
encode.StartInfo.RedirectStandardInput = false;
encode.StartInfo.RedirectStandardOutput = false;

encode.Start();

如上所述,终端可以正常打开,但不会对我发送给它的参数做任何事情。

我尝试过的事情:

  • 设置 RedirectsStandardInput = true 然后手动发送命令,但它从不响应。
  • 将 /c、-c 及其所有排列添加到我的参数中

提前感谢您的帮助。

【问题讨论】:

  • 为什么要打开终端应用才能玩 ffmpeg?
  • @Jason 因为 ffmpeg 包不适用于 xamarin。我已经尽我所能尝试了。这是我可以使用 ffmpeg 的唯一方法。
  • 不,我在问您为什么认为终端是解决方案的一部分。终端不运行任何命令,shell(zsh、bsh 等)会。
  • @Jason 如果我理解正确,解决方案应该是在解决方案的工作目录中包含一个 ffmpeg 版本,然后使用我的参数调用该 ffmpeg 文件以对我的文件进行操作。我已经工作了一整天了,所以我明天会尝试这个,如果成功了再告诉你。非常感谢您的帮助。

标签: c# visual-studio xamarin command-line command-line-arguments


【解决方案1】:

这是我找到的解决方案。它可能不是最好的,但它确实有效。

  1. 创建一个 bash 脚本,其中包含您希望终端执行的代码并让它接受您指定的参数。 https://unix.stackexchange.com/questions/31414/how-can-i-pass-a-command-line-argument-into-a-shell-script

(您可以在 MacOS 上创建一个文本文件并保存为 yourfile.exe,然后从终端运行 chmod +x yourfile.exe 以启用它。)

  1. 将文件放在解决方案的资源文件夹中。 (在解决方案资源管理器中右键单击资源 -> 添加 -> 现有文件。

  2. 使用此代码引用此文件:

string bashpath = System.IO.Directory.GetCurrentDirectory() + "/yourfile.exe";
string args = "your args here";

System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.StartInfo.FileName = bashpath;
proc.StartInfo.Arguments = args;
proc.Start();

要记住的事情:

  • 您的资源文件夹将您的 .app 文件中。右键单击->显示包内容。
  • Exit 0 添加到脚本末尾,以便调用 proc.WaitForExit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2023-03-05
    • 2015-04-04
    • 2013-11-19
    相关资源
    最近更新 更多