如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数

How to use Process.Start() or equivalent with Mono on a Mac and pass in arguments(如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数)
本文介绍了如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些 c# 代码来使用 Process.Start(app,args); 启动浏览器,其中应用程序是浏览器的路径,例如/Applications/Google Chrome.app/Contents/MacOS/Google Chrome 和 args 是 --no-default-browser-check

I am trying to write some c# code to start a browser using Process.Start(app,args); where apps is the path to the browser e.g. /Applications/Google Chrome.app/Contents/MacOS/Google Chrome and the args are --no-default-browser-check

如果我这样做,它适用于 Windows 和 Linux

If i do, which works on Windows and on Linux

Process.Start("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome","--no-first-run");

我明白了

open: unrecognized option `--no-first-run'
Usage: open [-e] [-t] [-f] [-W] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames]
Help: Open opens files from a shell.
      By default, opens each file using the default application for that file.  
      If the file is in the form of a URL, the file will be opened as a URL.
Options: 
      -a                Opens with the specified application.
      -b                Opens with the specified application bundle identifier.
      -e                Opens with TextEdit.
      -t                Opens with default text editor.
      -f                Reads input from standard input and opens with TextEdit.
      -W, --wait-apps   Blocks until the used applications are closed (even if they were already running).
      -n, --new         Open a new instance of the application even if one is already running.
      -g, --background  Does not bring the application to the foreground.
      -h, --header      Searches header file locations for headers matching the given filenames, and opens them.

我也尝试过 Monobjc 尝试使用

I have also tried Monobjc to try run the code with

// spin up the objective-c runtime
ObjectiveCRuntime.LoadFramework("Cocoa");
ObjectiveCRuntime.Initialize();
NSAutoreleasePool pool = new NSAutoreleasePool();

// Create our process
NSTask task = new NSTask();
NSPipe standardOut = new NSPipe();
task.StandardOutput = standardOut;
task.LaunchPath = @"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";

// add some arguments
NSString argumentString = new NSString("--no-first-run");
NSArray arguments = NSArray.ArrayWithObject(argumentString);
task.Arguments = arguments;

// We should have liftoff
task.Launch();


// Parse the output and display it to the console
NSData output = standardOut.FileHandleForReading.ReadDataToEndOfFile;
NSString outString = new NSString(output,NSStringEncoding.NSUTF8StringEncoding);
Console.WriteLine(outString);

// Dipose our objects, gotta love reference counting
pool.Release();

但是当我使用 NUnit 运行我的代码时,它会导致 NUnit 崩溃.

But when I run my code using NUnit it causes NUnit to blow up.

我怀疑这是一个错误,但无法证明.感谢您的任何帮助!

I suspect that this is a bug but can't prove it. I appreciate any and all help!

推荐答案

要让 Process.Start 直接使用 exec 而不是使用操作系统打开文件的机制,必须将 UseShellExecute 设置为 false.在 Linux 和 Windows 上也是如此.

To make Process.Start use exec directly instead of using the OS' mechanism for opening files, you must set UseShellExecute to false. This is also true on Linux and Windows.

Process.Start(new ProcessStartInfo (
    "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
    "--no-first-run")
    { UseShellExecute = false });

请注意,您也可以对您的用例使用打开",以正确运行 Chrome 应用程序包.使用-a"参数强制它运行特定的应用程序,使用-n"参数打开一个新实例,使用--args"传入参数:

Note that you can also use 'open' for your use case, to run the Chrome app bundle properly. Use the '-a' argument to force it to run a specific app, the '-n' argument to open a new instance, and '--args' to pass in arguments:

Process.Start(new ProcessStartInfo (
    "open",
    "-a '/Applications/Google Chrome.app' -n --args --no-first-run")
    { UseShellExecute = false });

这篇关于如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)